줄바꿈을 LF로 통일

서버에서 바로 빌드하고 고칠 수 있게 저장소 줄바꿈을 LF로 맞췄습니다.
CRLF로 들어가 있던 sh 빌드 스크립트도 이제 리눅스에서 그대로 돌아갑니다.
내용은 바뀌지 않았습니다.
This commit is contained in:
syskiller committed 2026-11-24 07:13:54 +09:00
1 parent 40615838ac
commit 2d48091205
623 files changed
+501372 -501365

No files matched your search

+41 -41
View File
@@ -1,41 +1,41 @@
# V3Emu
The Tamagotchi Connection V3 on the Tamagotchi Paradise. No ROM here: bring your own (512 KB).
- `src/core/v3cpu.c`, `v3cpu.h`: Sunplus SPLB32 (6502 with banked ROM, timers, 1 kHz time base, STOP, buzzer)
and the HT24LC08 EEPROM, ported from BrickEmuPy by azya52 (CC0), `cores/SPLB32.py` and
`peripherals/HT24LC08.py`. Time in ticks of 1/128 cycle, so the 32768 Hz clock is a whole number of ticks.
- `src/core/v3ops.inc`: the 256 instructions, generated by `gen/py2c.py` from `SPLB32.py`.
- `src/core/v3lcd.inc`: which bit of the LCD RAM lights which of the 32 x 30 dots, taken from the face
`assets/Tamagotchi_v3.svg` of BrickEmuPy (only the positions).
- `src/app/main.c`: the Paradise program. Settings block of V1Emu ("PMX1"), the Studio tells the two apart
by the ROM size. Save: session (4 KB), then settings and the toy's EEPROM (4 KB).
- `host/v3run.c`: runs a ROM on the computer, writes the screen as PGM (`TRACE`, `SHOTS`).
- `test/devv3.py`: the Paradise program in Unicorn (screen, keys, flash); `test/mkimg.py` makes a test image;
`test/profv3.py` counts instructions per function. `POKE=ms:symbol+off:byte` in devv3 writes a byte once, `PMB=seconds` puts the Paradise's time in the mailbox,
`DUMP=symbol+off:len` prints memory at the end. `host/v3run` takes `RX=t:hex` (bytes into the receiver) and prints sent bytes.
Build: `sh src/app/build.sh` (clang, lld) gives `v3.bin`.
Clock (V3_CLK_RETRY, test37): a standing clock (the toy started from its own save and asks for the time) is looked at
again every 5 s for 10 minutes and set as soon as it runs; the settings line "Clock" shows AUTO OK, AUTO ! (stands),
AUTO - (no time from the Paradise) or AUTO ? (written but off).
Switches in the program: `V3_CLOCK` (the Paradise's time at the start), `V3_LINK` (infrared over the contacts), `V3_ICONS` (the ten icons, segments 0_7, 1_0..1_3 above and 6_3..6_0, 5_7 below; pictures in
`src/app/icons_v3.h`, made by `gen/icons.py` from mameise's PNGs; a missing one is taken from T7Emu).
The core's infrared (`v3_ir_rx`, `v3_ir_tx`) was checked against CON_V3_IR.py with the same script of port writes
and clock steps (sent bytes and receiver level the same).
Switches in the core: `V3_FREESTANDING` (no libc), `V3_BATCH` (0: no skipping while stopped, for comparing),
`V3_TMFAST` (1.6.13 test12: the timers in closed form and 32-bit arithmetic; an event whose flags are masked is left
to the next sync, the flag registers sync when read or written; checked equal to the plain core on the host, step by
step and in 1 ms runs, `V3_SAVESYNC` makes the plain core sync before a save for that comparison).
Power (`V3_PW`, test12, as TamaEmu `EMU_PW`): screen dark, no key, tone, menu or link traffic: 12 MHz from the
internal RC, deep sleep until the next RTC second or a key, then the toy's time runs in one go (up to 5 s behind,
never dropped). A tone while dark: from there in real time. `BAT_SMOOTH`: steadier battery reading.
Author: mameise
No session (SESS_FLAG, 1.7.3 test7): flags bit 12 (0x1000): the run state is never written and an old one is dropped at the start; the toy's EEPROM and the settings are stored as before, so it starts from its own save like the real toy.
# V3Emu
The Tamagotchi Connection V3 on the Tamagotchi Paradise. No ROM here: bring your own (512 KB).
- `src/core/v3cpu.c`, `v3cpu.h`: Sunplus SPLB32 (6502 with banked ROM, timers, 1 kHz time base, STOP, buzzer)
and the HT24LC08 EEPROM, ported from BrickEmuPy by azya52 (CC0), `cores/SPLB32.py` and
`peripherals/HT24LC08.py`. Time in ticks of 1/128 cycle, so the 32768 Hz clock is a whole number of ticks.
- `src/core/v3ops.inc`: the 256 instructions, generated by `gen/py2c.py` from `SPLB32.py`.
- `src/core/v3lcd.inc`: which bit of the LCD RAM lights which of the 32 x 30 dots, taken from the face
`assets/Tamagotchi_v3.svg` of BrickEmuPy (only the positions).
- `src/app/main.c`: the Paradise program. Settings block of V1Emu ("PMX1"), the Studio tells the two apart
by the ROM size. Save: session (4 KB), then settings and the toy's EEPROM (4 KB).
- `host/v3run.c`: runs a ROM on the computer, writes the screen as PGM (`TRACE`, `SHOTS`).
- `test/devv3.py`: the Paradise program in Unicorn (screen, keys, flash); `test/mkimg.py` makes a test image;
`test/profv3.py` counts instructions per function. `POKE=ms:symbol+off:byte` in devv3 writes a byte once, `PMB=seconds` puts the Paradise's time in the mailbox,
`DUMP=symbol+off:len` prints memory at the end. `host/v3run` takes `RX=t:hex` (bytes into the receiver) and prints sent bytes.
Build: `sh src/app/build.sh` (clang, lld) gives `v3.bin`.
Clock (V3_CLK_RETRY, test37): a standing clock (the toy started from its own save and asks for the time) is looked at
again every 5 s for 10 minutes and set as soon as it runs; the settings line "Clock" shows AUTO OK, AUTO ! (stands),
AUTO - (no time from the Paradise) or AUTO ? (written but off).
Switches in the program: `V3_CLOCK` (the Paradise's time at the start), `V3_LINK` (infrared over the contacts), `V3_ICONS` (the ten icons, segments 0_7, 1_0..1_3 above and 6_3..6_0, 5_7 below; pictures in
`src/app/icons_v3.h`, made by `gen/icons.py` from mameise's PNGs; a missing one is taken from T7Emu).
The core's infrared (`v3_ir_rx`, `v3_ir_tx`) was checked against CON_V3_IR.py with the same script of port writes
and clock steps (sent bytes and receiver level the same).
Switches in the core: `V3_FREESTANDING` (no libc), `V3_BATCH` (0: no skipping while stopped, for comparing),
`V3_TMFAST` (1.6.13 test12: the timers in closed form and 32-bit arithmetic; an event whose flags are masked is left
to the next sync, the flag registers sync when read or written; checked equal to the plain core on the host, step by
step and in 1 ms runs, `V3_SAVESYNC` makes the plain core sync before a save for that comparison).
Power (`V3_PW`, test12, as TamaEmu `EMU_PW`): screen dark, no key, tone, menu or link traffic: 12 MHz from the
internal RC, deep sleep until the next RTC second or a key, then the toy's time runs in one go (up to 5 s behind,
never dropped). A tone while dark: from there in real time. `BAT_SMOOTH`: steadier battery reading.
Author: mameise
No session (SESS_FLAG, 1.7.3 test7): flags bit 12 (0x1000): the run state is never written and an old one is dropped at the start; the toy's EEPROM and the settings are stored as before, so it starts from its own save like the real toy.
+23 -23
View File
@@ -1,23 +1,23 @@
# The ten V3Emu icons (20 x 20 PNG, mameise) -> src/app/icons_v3.h. A missing icon is taken from T7Emu (t7/src/app/icons.h).
# python3 gen/icons.py <folder with icon_0.png .. icon_9.png (icon_06.png also works)>
import sys, os, re
from PIL import Image
d = sys.argv[1]
t7 = re.findall(r'\{([^{}]+)\}', open(os.path.join(os.path.dirname(__file__), '../../t7/src/app/icons.h')).read())
rows = []
for i in range(10):
f = next((os.path.join(d, n) for n in ('icon_%d.png' % i, 'icon_%02d.png' % i) if os.path.exists(os.path.join(d, n))), None)
if not f:
rows.append(t7[i].strip()); print('icon %d: from T7Emu' % i); continue
im = Image.open(f).convert('RGBA').resize((20, 20))
v = []
for y in range(20):
b = 0
for x in range(20):
p = im.getpixel((x, y))
if p[3] > 128 and sum(p[:3]) < 384: b |= 0x80000 >> x
v.append('0x%08X' % b)
rows.append(','.join(v)); print('icon %d: %s' % (i, os.path.basename(f)))
with open(os.path.join(os.path.dirname(__file__), '../src/app/icons_v3.h'), 'w') as o:
o.write('/* 20x20 icons of V3Emu (bit 0x80000 = left), drawn by mameise; made by gen/icons.py */\n')
o.write('static const unsigned int g_icons[10][20]={\n' + ''.join(' {%s},\n' % r for r in rows) + '};\n')
# The ten V3Emu icons (20 x 20 PNG, mameise) -> src/app/icons_v3.h. A missing icon is taken from T7Emu (t7/src/app/icons.h).
# python3 gen/icons.py <folder with icon_0.png .. icon_9.png (icon_06.png also works)>
import sys, os, re
from PIL import Image
d = sys.argv[1]
t7 = re.findall(r'\{([^{}]+)\}', open(os.path.join(os.path.dirname(__file__), '../../t7/src/app/icons.h')).read())
rows = []
for i in range(10):
f = next((os.path.join(d, n) for n in ('icon_%d.png' % i, 'icon_%02d.png' % i) if os.path.exists(os.path.join(d, n))), None)
if not f:
rows.append(t7[i].strip()); print('icon %d: from T7Emu' % i); continue
im = Image.open(f).convert('RGBA').resize((20, 20))
v = []
for y in range(20):
b = 0
for x in range(20):
p = im.getpixel((x, y))
if p[3] > 128 and sum(p[:3]) < 384: b |= 0x80000 >> x
v.append('0x%08X' % b)
rows.append(','.join(v)); print('icon %d: %s' % (i, os.path.basename(f)))
with open(os.path.join(os.path.dirname(__file__), '../src/app/icons_v3.h'), 'w') as o:
o.write('/* 20x20 icons of V3Emu (bit 0x80000 = left), drawn by mameise; made by gen/icons.py */\n')
o.write('static const unsigned int g_icons[10][20]={\n' + ''.join(' {%s},\n' % r for r in rows) + '};\n')
+65 -65
View File
@@ -1,65 +1,65 @@
# py2c.py - translates the instruction methods of BrickEmuPy's SPLB32 core (cores/SPLB32.py, CC0) into C.
# python3 py2c.py <BrickEmuPy dir> > ../src/core/v3ops.inc
# Each method becomes "static int op_XX(void)" on the state macros of v3cpu.c (A, X, Y, SP, PC, NF ... RD, WR).
import re, sys, inspect
sys.path.insert(0, sys.argv[1])
from cores.SPLB32 import SPLB32
src = open(sys.argv[1] + '/cores/SPLB32.py').read()
t = src[src.index('self._execute = ('):src.index('def _get_rom_addr')]
ops = []
for a, n, b in re.findall(r'\*\(\[SPLB32\.(_\w+)\] \* (\d+)\)|SPLB32\.(_\w+)', t):
ops += [a] * int(n) if a else [b]
assert len(ops) == 256
def expr(e):
e = e.strip()
e = re.sub(r'(?<![_\w])SP\b', '0x100', e)
e = re.sub(r'self\._read_mem\(', 'RD(', e)
e = re.sub(r'self\._write_mem\(', 'WR(', e)
e = re.sub(r'self\._get_ps\(\)', 'get_ps()', e)
e = re.sub(r'self\._set_ps\(', 'set_ps(', e)
e = re.sub(r'self\._adc\(', 'do_adc(', e)
e = re.sub(r'self\._sbc\(', 'do_sbc(', e)
e = re.sub(r'self\._IRQ\(\)', 'irq_enter()', e)
e = re.sub(r'self\._([A-Z][A-Z_0-9]*)', r'\1', e)
e = re.sub(r'(\w+) & (0x[0-9A-Fa-f]+) > 0', r'(((\1) & \2) > 0)', e)
e = re.sub(r'\bnot\s*\(', '!(', e)
e = re.sub(r'\bnot\s+([\w.]+)', r'!(\1)', e)
e = e.replace(' and ', ' && ').replace(' or ', ' || ')
return e
out = []
done = {}
for i, name in enumerate(ops):
if name == '_dummy' or name in done: continue
lines = inspect.getsource(getattr(SPLB32, name)).split('\n')[1:]
body = []; stack = [8]
for l in lines:
if not l.strip(): continue
ind = len(l) - len(l.lstrip()); s = l.strip()
while ind < stack[-1]: stack.pop(); body.append(' ' * (len(stack)) + '}')
pre = ' ' * len(stack)
m = re.match(r'(if|elif|while) \((.*)\):$', s) or re.match(r'(if|elif|while) (.*):$', s)
if m:
kw = {'if': 'if', 'elif': 'else if', 'while': 'while'}[m.group(1)]
body.append(pre + '%s (%s) {' % (kw, expr(m.group(2)))); stack.append(ind + 4); continue
if s == 'else:':
body.append(pre + 'else {'); stack.append(ind + 4); continue
m = re.match(r'return (.*)$', s)
if m: body.append(pre + 'return (int)(%s);' % expr(m.group(1))); continue
m = re.match(r'([\w.]+) *(\|=|\^=|&=|\+=|-=|=) *(.*)$', s)
if m:
lhs = expr(m.group(1)); rhs = expr(m.group(3))
if lhs in ('zp', 'addr', 'base', 'new_value', 'prev_value', 'test_value', 'm', 'rel', 'prev_PC', 'pc', 'opcode', 'value') and m.group(2) == '=' and lhs not in done.get('_vars_' + name, set()):
done.setdefault('_vars_' + name, set()).add(lhs)
body.append(pre + 'int %s = %s;' % (lhs, rhs))
else:
body.append(pre + '%s %s %s;' % (lhs, m.group(2), rhs))
continue
body.append(pre + expr(s) + ';')
while len(stack) > 1: stack.pop(); body.append(' ' * len(stack) + '}')
done[name] = 1
out.append('static int op%s(void)\n{\n%s\n}' % (name, '\n'.join(body)))
print('/* generated by v3/gen/py2c.py from BrickEmuPy cores/SPLB32.py (azya52, CC0) */')
print('\n'.join(out))
print('static int (*const v3_op[256])(void) = {')
print(',\n'.join(' ' + ('op_dummy' if n == '_dummy' else 'op' + n) for n in ops))
print('};')
# py2c.py - translates the instruction methods of BrickEmuPy's SPLB32 core (cores/SPLB32.py, CC0) into C.
# python3 py2c.py <BrickEmuPy dir> > ../src/core/v3ops.inc
# Each method becomes "static int op_XX(void)" on the state macros of v3cpu.c (A, X, Y, SP, PC, NF ... RD, WR).
import re, sys, inspect
sys.path.insert(0, sys.argv[1])
from cores.SPLB32 import SPLB32
src = open(sys.argv[1] + '/cores/SPLB32.py').read()
t = src[src.index('self._execute = ('):src.index('def _get_rom_addr')]
ops = []
for a, n, b in re.findall(r'\*\(\[SPLB32\.(_\w+)\] \* (\d+)\)|SPLB32\.(_\w+)', t):
ops += [a] * int(n) if a else [b]
assert len(ops) == 256
def expr(e):
e = e.strip()
e = re.sub(r'(?<![_\w])SP\b', '0x100', e)
e = re.sub(r'self\._read_mem\(', 'RD(', e)
e = re.sub(r'self\._write_mem\(', 'WR(', e)
e = re.sub(r'self\._get_ps\(\)', 'get_ps()', e)
e = re.sub(r'self\._set_ps\(', 'set_ps(', e)
e = re.sub(r'self\._adc\(', 'do_adc(', e)
e = re.sub(r'self\._sbc\(', 'do_sbc(', e)
e = re.sub(r'self\._IRQ\(\)', 'irq_enter()', e)
e = re.sub(r'self\._([A-Z][A-Z_0-9]*)', r'\1', e)
e = re.sub(r'(\w+) & (0x[0-9A-Fa-f]+) > 0', r'(((\1) & \2) > 0)', e)
e = re.sub(r'\bnot\s*\(', '!(', e)
e = re.sub(r'\bnot\s+([\w.]+)', r'!(\1)', e)
e = e.replace(' and ', ' && ').replace(' or ', ' || ')
return e
out = []
done = {}
for i, name in enumerate(ops):
if name == '_dummy' or name in done: continue
lines = inspect.getsource(getattr(SPLB32, name)).split('\n')[1:]
body = []; stack = [8]
for l in lines:
if not l.strip(): continue
ind = len(l) - len(l.lstrip()); s = l.strip()
while ind < stack[-1]: stack.pop(); body.append(' ' * (len(stack)) + '}')
pre = ' ' * len(stack)
m = re.match(r'(if|elif|while) \((.*)\):$', s) or re.match(r'(if|elif|while) (.*):$', s)
if m:
kw = {'if': 'if', 'elif': 'else if', 'while': 'while'}[m.group(1)]
body.append(pre + '%s (%s) {' % (kw, expr(m.group(2)))); stack.append(ind + 4); continue
if s == 'else:':
body.append(pre + 'else {'); stack.append(ind + 4); continue
m = re.match(r'return (.*)$', s)
if m: body.append(pre + 'return (int)(%s);' % expr(m.group(1))); continue
m = re.match(r'([\w.]+) *(\|=|\^=|&=|\+=|-=|=) *(.*)$', s)
if m:
lhs = expr(m.group(1)); rhs = expr(m.group(3))
if lhs in ('zp', 'addr', 'base', 'new_value', 'prev_value', 'test_value', 'm', 'rel', 'prev_PC', 'pc', 'opcode', 'value') and m.group(2) == '=' and lhs not in done.get('_vars_' + name, set()):
done.setdefault('_vars_' + name, set()).add(lhs)
body.append(pre + 'int %s = %s;' % (lhs, rhs))
else:
body.append(pre + '%s %s %s;' % (lhs, m.group(2), rhs))
continue
body.append(pre + expr(s) + ';')
while len(stack) > 1: stack.pop(); body.append(' ' * len(stack) + '}')
done[name] = 1
out.append('static int op%s(void)\n{\n%s\n}' % (name, '\n'.join(body)))
print('/* generated by v3/gen/py2c.py from BrickEmuPy cores/SPLB32.py (azya52, CC0) */')
print('\n'.join(out))
print('static int (*const v3_op[256])(void) = {')
print(',\n'.join(' ' + ('op_dummy' if n == '_dummy' else 'op' + n) for n in ops))
print('};')
+42 -42
View File
@@ -1,42 +1,42 @@
/* PC run of the V3 core: v3run <rom> <seconds> <prefix> [keys "t:mask,..."]
* env TRACE=n: the first n steps as "n pc a x y sp NVDIZC" (as BrickEmuPy, see the reference ref.py)
* env SHOTS=ms: a 32x30 PGM every ms. Author: mameise */
#include "../src/core/v3cpu.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static uint16_t g_map[30][32]; /* LCD RAM byte << 3 | bit for each pixel */
#include "../src/core/v3lcd.inc"
static void shot(const char *fn)
{
int mode; const uint8_t *v = v3_vram(&mode);
FILE *o = fopen(fn, "wb"); fprintf(o, "P5 32 30 255\n");
for (int y = 0; y < 30; y++) for (int x = 0; x < 32; x++) { int k = V3_LCD[y][x], on = mode == 2 || (mode == 1 && (v[k >> 3] >> (k & 7) & 1)); fputc(on ? 0 : 255, o); }
fclose(o);
}
int main(int c, char **v)
{
FILE *f = fopen(v[1], "rb"); fseek(f, 0, 2); long n = ftell(f); fseek(f, 0, 0); uint8_t *rom = malloc(n); if (fread(rom, 1, n, f)) {} fclose(f);
v3_init(rom, (uint32_t)n);
double secs = atof(v[2]); const char *pre = v[3];
struct { double t; unsigned m; } ks[64]; int nk = 0, ki = 0;
if (c > 4) { char *s = strdup(v[4]); for (char *p = strtok(s, ","); p && nk < 64; p = strtok(0, ",")) { ks[nk].t = atof(p); ks[nk].m = (unsigned)strtoul(strchr(p, ':') + 1, 0, 0); nk++; } }
/* env RX="t:hexbytes,...": bytes into the infrared receiver at time t (s); bytes sent are printed as "TX t hex" */
struct { double t; uint8_t b[32]; int n; } rx[16]; int nrx = 0, ri = 0;
if (getenv("RX")) { char *s = strdup(getenv("RX")); for (char *p = strtok(s, ","); p && nrx < 16; p = strtok(0, ",")) { rx[nrx].t = atof(p); const char *h = strchr(p, ':') + 1; rx[nrx].n = 0; while (h[0] && h[1] && rx[nrx].n < 32) { unsigned x; sscanf(h, "%2x", &x); rx[nrx].b[rx[nrx].n++] = (uint8_t)x; h += 2; } nrx++; } }
long tfrom = getenv("TRACEFROM") ? atol(getenv("TRACEFROM")) : 0;
long tr = getenv("TRACE") ? atol(getenv("TRACE")) : 0, shots = getenv("SHOTS") ? atol(getenv("SHOTS")) : 0;
FILE *tf = 0; if (tr) { char fn[256]; snprintf(fn, sizeof fn, "%s_trace.txt", pre); tf = fopen(fn, "w"); }
uint64_t total = (uint64_t)(secs * V3_CLOCK) * V3_TICKS, t = 0, next = 0; long i = 0; int si = 0;
while (t < total) {
while (ki < nk && t >= (uint64_t)(ks[ki].t * V3_CLOCK) * V3_TICKS) v3_set_keys(ks[ki++].m);
while (ri < nrx && t >= (uint64_t)(rx[ri].t * V3_CLOCK) * V3_TICKS) { v3_ir_rx(rx[ri].b, rx[ri].n); ri++; }
if (tf && i >= tfrom && i < tfrom + tr) { uint8_t r[5], fl[6]; v3_regs(r, fl); fprintf(tf, "%ld %04x %02x %02x %02x %02x %d%d%d%d%d%d\n", i, v3_pc(), r[0], r[1], r[2], r[3], fl[0], fl[1], fl[2], fl[3], fl[4], fl[5]); }
t += v3_step(); i++;
{ uint8_t o[32]; int n = v3_ir_tx(o, 32); for (int k = 0; k < n; k++) printf("TX %.4f %02x\n", (double)t / V3_TICKS / V3_CLOCK, o[k]); }
if (shots && t >= next) { char fn[256]; snprintf(fn, sizeof fn, "%s_%03d.pgm", pre, si++); shot(fn); next += (uint64_t)V3_CLOCK * V3_TICKS * (uint64_t)shots / 1000u; }
}
char fn[256]; snprintf(fn, sizeof fn, "%s_end.pgm", pre); shot(fn);
printf("steps %ld pc %04x bank %u tone %u\n", i, v3_pc(), v3_bank(), v3_tone_hz());
return 0;
}
/* PC run of the V3 core: v3run <rom> <seconds> <prefix> [keys "t:mask,..."]
* env TRACE=n: the first n steps as "n pc a x y sp NVDIZC" (as BrickEmuPy, see the reference ref.py)
* env SHOTS=ms: a 32x30 PGM every ms. Author: mameise */
#include "../src/core/v3cpu.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static uint16_t g_map[30][32]; /* LCD RAM byte << 3 | bit for each pixel */
#include "../src/core/v3lcd.inc"
static void shot(const char *fn)
{
int mode; const uint8_t *v = v3_vram(&mode);
FILE *o = fopen(fn, "wb"); fprintf(o, "P5 32 30 255\n");
for (int y = 0; y < 30; y++) for (int x = 0; x < 32; x++) { int k = V3_LCD[y][x], on = mode == 2 || (mode == 1 && (v[k >> 3] >> (k & 7) & 1)); fputc(on ? 0 : 255, o); }
fclose(o);
}
int main(int c, char **v)
{
FILE *f = fopen(v[1], "rb"); fseek(f, 0, 2); long n = ftell(f); fseek(f, 0, 0); uint8_t *rom = malloc(n); if (fread(rom, 1, n, f)) {} fclose(f);
v3_init(rom, (uint32_t)n);
double secs = atof(v[2]); const char *pre = v[3];
struct { double t; unsigned m; } ks[64]; int nk = 0, ki = 0;
if (c > 4) { char *s = strdup(v[4]); for (char *p = strtok(s, ","); p && nk < 64; p = strtok(0, ",")) { ks[nk].t = atof(p); ks[nk].m = (unsigned)strtoul(strchr(p, ':') + 1, 0, 0); nk++; } }
/* env RX="t:hexbytes,...": bytes into the infrared receiver at time t (s); bytes sent are printed as "TX t hex" */
struct { double t; uint8_t b[32]; int n; } rx[16]; int nrx = 0, ri = 0;
if (getenv("RX")) { char *s = strdup(getenv("RX")); for (char *p = strtok(s, ","); p && nrx < 16; p = strtok(0, ",")) { rx[nrx].t = atof(p); const char *h = strchr(p, ':') + 1; rx[nrx].n = 0; while (h[0] && h[1] && rx[nrx].n < 32) { unsigned x; sscanf(h, "%2x", &x); rx[nrx].b[rx[nrx].n++] = (uint8_t)x; h += 2; } nrx++; } }
long tfrom = getenv("TRACEFROM") ? atol(getenv("TRACEFROM")) : 0;
long tr = getenv("TRACE") ? atol(getenv("TRACE")) : 0, shots = getenv("SHOTS") ? atol(getenv("SHOTS")) : 0;
FILE *tf = 0; if (tr) { char fn[256]; snprintf(fn, sizeof fn, "%s_trace.txt", pre); tf = fopen(fn, "w"); }
uint64_t total = (uint64_t)(secs * V3_CLOCK) * V3_TICKS, t = 0, next = 0; long i = 0; int si = 0;
while (t < total) {
while (ki < nk && t >= (uint64_t)(ks[ki].t * V3_CLOCK) * V3_TICKS) v3_set_keys(ks[ki++].m);
while (ri < nrx && t >= (uint64_t)(rx[ri].t * V3_CLOCK) * V3_TICKS) { v3_ir_rx(rx[ri].b, rx[ri].n); ri++; }
if (tf && i >= tfrom && i < tfrom + tr) { uint8_t r[5], fl[6]; v3_regs(r, fl); fprintf(tf, "%ld %04x %02x %02x %02x %02x %d%d%d%d%d%d\n", i, v3_pc(), r[0], r[1], r[2], r[3], fl[0], fl[1], fl[2], fl[3], fl[4], fl[5]); }
t += v3_step(); i++;
{ uint8_t o[32]; int n = v3_ir_tx(o, 32); for (int k = 0; k < n; k++) printf("TX %.4f %02x\n", (double)t / V3_TICKS / V3_CLOCK, o[k]); }
if (shots && t >= next) { char fn[256]; snprintf(fn, sizeof fn, "%s_%03d.pgm", pre, si++); shot(fn); next += (uint64_t)V3_CLOCK * V3_TICKS * (uint64_t)shots / 1000u; }
}
char fn[256]; snprintf(fn, sizeof fn, "%s_end.pgm", pre); shot(fn);
printf("steps %ld pc %04x bank %u tone %u\n", i, v3_pc(), v3_bank(), v3_tone_hz());
return 0;
}
+12 -12
View File
@@ -1,12 +1,12 @@
#!/bin/sh
# Build V3Emu for the Paradise (clang/lld, Cortex-M3). Output: ../../v3.bin
set -e
cd "$(dirname "$0")"
E=../../../emu/src/emu
CF="--target=thumbv7m-none-eabi -mcpu=cortex-m3 -mthumb -ffreestanding -nostdlib -fno-common -Wall -Wno-unused-function -Wno-unused-variable -fno-builtin-memset -DFAST_LCD -DEXIT_KEY -DLCD_FASTCLK -DTONE_FIX -DHW_TONE_RESTART -DWAKE_WHEEL -DBL_SLEEP -DWAKE_FRESH -DSESS_ONCE -DPARA_BACK -DPARA_MB_KEEP -DBL_PWM -DBL_MR3RST -DBAT_SHOW -DV3_FREESTANDING -DV3_ICONS -DV3_LINK -DV3_CLOCK -DCLK_ECO -DECO_RTC -DV3_SETX -DV3_CARE -DASK_NUM -DV3_CLK_RETRY -DPARA_FLASH -DPARA_FLASH_LATE -DPARA_CLEAN_CLK -DV3_TMFAST -DV3_PW -DBAT_SMOOTH -DHW_OPTS -DV3_VOL -DV3_TONE_Q -DV3_CALL_WAKE -DTONE_SAFE -DV3_EE2 -DBAT_GUARD -DBAT_PIC -DSESS_FLAG -I../core $XCF"
O=""
for f in main.c $E/hw.c $E/ask.c $E/flash.c $E/libc.c ../core/v3cpu.c; do
n=$(basename $f .c); OPT=-Os; [ "$n" = v3cpu ] && OPT=-O2; clang $CF $OPT -c $f -o $n.o; O="$O $n.o"; done
ld.lld -T link.ld --no-rosegment $O -o v3.elf
llvm-objcopy -O binary -j .text -j .data -j .init v3.elf v3.bin
cp v3.bin ../../v3.bin; rm -f *.o v3.bin; [ -n "$KEEPELF" ] || rm -f v3.elf; ls -l ../../v3.bin
#!/bin/sh
# Build V3Emu for the Paradise (clang/lld, Cortex-M3). Output: ../../v3.bin
set -e
cd "$(dirname "$0")"
E=../../../emu/src/emu
CF="--target=thumbv7m-none-eabi -mcpu=cortex-m3 -mthumb -ffreestanding -nostdlib -fno-common -Wall -Wno-unused-function -Wno-unused-variable -fno-builtin-memset -DFAST_LCD -DEXIT_KEY -DLCD_FASTCLK -DTONE_FIX -DHW_TONE_RESTART -DWAKE_WHEEL -DBL_SLEEP -DWAKE_FRESH -DSESS_ONCE -DPARA_BACK -DPARA_MB_KEEP -DBL_PWM -DBL_MR3RST -DBAT_SHOW -DV3_FREESTANDING -DV3_ICONS -DV3_LINK -DV3_CLOCK -DCLK_ECO -DECO_RTC -DV3_SETX -DV3_CARE -DASK_NUM -DV3_CLK_RETRY -DPARA_FLASH -DPARA_FLASH_LATE -DPARA_CLEAN_CLK -DV3_TMFAST -DV3_PW -DBAT_SMOOTH -DHW_OPTS -DV3_VOL -DV3_TONE_Q -DV3_CALL_WAKE -DTONE_SAFE -DV3_EE2 -DBAT_GUARD -DBAT_PIC -DSESS_FLAG -I../core $XCF"
O=""
for f in main.c $E/hw.c $E/ask.c $E/flash.c $E/libc.c ../core/v3cpu.c; do
n=$(basename $f .c); OPT=-Os; [ "$n" = v3cpu ] && OPT=-O2; clang $CF $OPT -c $f -o $n.o; O="$O $n.o"; done
ld.lld -T link.ld --no-rosegment $O -o v3.elf
llvm-objcopy -O binary -j .text -j .data -j .init v3.elf v3.bin
cp v3.bin ../../v3.bin; rm -f *.o v3.bin; [ -n "$KEEPELF" ] || rm -f v3.elf; ls -l ../../v3.bin
+99 -99
View File
@@ -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
View File
@@ -1,13 +1,13 @@
/* 20x20 icons of V3Emu (bit 0x80000 = left), drawn by mameise; made by gen/icons.py */
static const unsigned int g_icons[10][20]={
{0x00000000,0x00000000,0x00000000,0x00000000,0x00001F80,0x0000E070,0x00010008,0x00014928,0x00012048,0x00010008,0x00010608,0x00008F10,0x00009F90,0x00007FE0,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,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,0x0000F000,0x00010800,0x00016800,0x00016838,0x00010878,0x0000F0F8,0x000001F0,0x000003E0,0x000007C0,0x00000F80,0x00000F00,0x00001E00,0x0000F800,0x00007000,0x00003000,0x00001000,0x00000000,0x00000000},
{0x00000000,0x00000000,0x00000008,0x00000004,0x00000012,0x00000009,0x00000024,0x00000010,0x000030C0,0x000079E0,0x0000FFF0,0x0000FFF0,0x0000FFF0,0x0000FFF0,0x00007FE0,0x00003FC0,0x00001F80,0x00000F00,0x00000600,0x00000000},
{0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00007E18,0x0000FF60,0x0001FE00,0x0003FC00,0x0003F87C,0x0003F000,0x0003F000,0x0003F840,0x0003FC20,0x0001FE10,0x0000FF00,0x00007E00,0x00000000,0x00000000,0x00000000},
{0x00000000,0x00000000,0x00000000,0x00000F00,0x00001080,0x00002040,0x00002040,0x00007FE0,0x00008010,0x00010608,0x00010608,0x00010608,0x00013FC8,0x00013FC8,0x00010608,0x00010608,0x00010608,0x00010008,0x0000FFF0,0x00000000},
{0x00000000,0x00000000,0x00000000,0x00001F80,0x00002040,0x00004020,0x00008010,0x00008010,0x0000C030,0x00007FE0,0x00002600,0x00000600,0x00000600,0x00003FC0,0x00007FE0,0x00003FC0,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,0x00000000,0x00003800,0x0000C600,0x00008200,0x00012900,0x00010100,0x00014570,0x0001388C,0x00008004,0x0000C482,0x00003AFA,0x00000252,0x00000104,0x0000018C,0x00000070,0x00000000,0x00000000},
};
/* 20x20 icons of V3Emu (bit 0x80000 = left), drawn by mameise; made by gen/icons.py */
static const unsigned int g_icons[10][20]={
{0x00000000,0x00000000,0x00000000,0x00000000,0x00001F80,0x0000E070,0x00010008,0x00014928,0x00012048,0x00010008,0x00010608,0x00008F10,0x00009F90,0x00007FE0,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,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,0x0000F000,0x00010800,0x00016800,0x00016838,0x00010878,0x0000F0F8,0x000001F0,0x000003E0,0x000007C0,0x00000F80,0x00000F00,0x00001E00,0x0000F800,0x00007000,0x00003000,0x00001000,0x00000000,0x00000000},
{0x00000000,0x00000000,0x00000008,0x00000004,0x00000012,0x00000009,0x00000024,0x00000010,0x000030C0,0x000079E0,0x0000FFF0,0x0000FFF0,0x0000FFF0,0x0000FFF0,0x00007FE0,0x00003FC0,0x00001F80,0x00000F00,0x00000600,0x00000000},
{0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00007E18,0x0000FF60,0x0001FE00,0x0003FC00,0x0003F87C,0x0003F000,0x0003F000,0x0003F840,0x0003FC20,0x0001FE10,0x0000FF00,0x00007E00,0x00000000,0x00000000,0x00000000},
{0x00000000,0x00000000,0x00000000,0x00000F00,0x00001080,0x00002040,0x00002040,0x00007FE0,0x00008010,0x00010608,0x00010608,0x00010608,0x00013FC8,0x00013FC8,0x00010608,0x00010608,0x00010608,0x00010008,0x0000FFF0,0x00000000},
{0x00000000,0x00000000,0x00000000,0x00001F80,0x00002040,0x00004020,0x00008010,0x00008010,0x0000C030,0x00007FE0,0x00002600,0x00000600,0x00000600,0x00003FC0,0x00007FE0,0x00003FC0,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,0x00000000,0x00003800,0x0000C600,0x00008200,0x00012900,0x00010100,0x00014570,0x0001388C,0x00008004,0x0000C482,0x00003AFA,0x00000252,0x00000104,0x0000018C,0x00000070,0x00000000,0x00000000},
};
+23 -23
View File
@@ -1,23 +1,23 @@
ENTRY(Reset_Handler)
MEMORY {
PRAM (rwx) : ORIGIN = 0x00000000, LENGTH = 0x10000
SRAM (rw) : ORIGIN = 0x18000000, LENGTH = 0x1F000
}
SECTIONS {
.text : {
KEEP(*(.vectors))
KEEP(*(.pmxcfg))
*(.text*)
*(.rodata*)
. = ALIGN(4);
__pdata_start = .;
} > PRAM
.bss (NOLOAD) : { __bss_start = .; *(.bss*) *(COMMON) . = ALIGN(4); __bss_end = .; } > SRAM
/* mutable data in SRAM (initial values in PRAM, copied at startup), as in TamaEmu */
.data : { __data_start = .; *(.data*) . = ALIGN(4); __data_end = .; } > SRAM AT > PRAM
__data_load = LOADADDR(.data);
.init : { . = ALIGN(4); KEEP(*(.initcode)) KEEP(*(.initdata*)) KEEP(*(.initcode_z)) . = ALIGN(4); __prog_end = .; } > PRAM
ASSERT(__prog_end <= 0x10000, "program too big for PRAM")
ASSERT(__bss_end <= 0x1801F000, "RAM full")
/DISCARD/ : { *(.comment) *(.ARM.*) *(.note*) }
}
ENTRY(Reset_Handler)
MEMORY {
PRAM (rwx) : ORIGIN = 0x00000000, LENGTH = 0x10000
SRAM (rw) : ORIGIN = 0x18000000, LENGTH = 0x1F000
}
SECTIONS {
.text : {
KEEP(*(.vectors))
KEEP(*(.pmxcfg))
*(.text*)
*(.rodata*)
. = ALIGN(4);
__pdata_start = .;
} > PRAM
.bss (NOLOAD) : { __bss_start = .; *(.bss*) *(COMMON) . = ALIGN(4); __bss_end = .; } > SRAM
/* mutable data in SRAM (initial values in PRAM, copied at startup), as in TamaEmu */
.data : { __data_start = .; *(.data*) . = ALIGN(4); __data_end = .; } > SRAM AT > PRAM
__data_load = LOADADDR(.data);
.init : { . = ALIGN(4); KEEP(*(.initcode)) KEEP(*(.initdata*)) KEEP(*(.initcode_z)) . = ALIGN(4); __prog_end = .; } > PRAM
ASSERT(__prog_end <= 0x10000, "program too big for PRAM")
ASSERT(__bss_end <= 0x1801F000, "RAM full")
/DISCARD/ : { *(.comment) *(.ARM.*) *(.note*) }
}
+897 -897
View File
File diff suppressed because it is too large. Load diff
+563 -563
View File
File diff suppressed because it is too large. Load diff
+30 -30
View File
@@ -1,30 +1,30 @@
/* v3cpu.h - Tamagotchi Connection V3 (Sunplus SPLB32, a 6502) in C, ported from BrickEmuPy (azya52, CC0):
* cores/SPLB32.py, peripherals/HT24LC08.py (EEPROM), direct_input.py (keys). Time in ticks of 1/128 cycle
* (4 MHz main clock, the 32768 Hz clock is exactly 15625 ticks), so the timers count as the Python floats did.
* Author: mameise */
#ifndef V3CPU_H
#define V3CPU_H
#include <stdint.h>
#define V3_CLOCK 4000000u
#define V3_TICKS 128u /* ticks per cycle */
#define V3_EE_SIZE 1024u
#define V3_DPRAM 0x200u
void v3_init(const uint8_t *rom, uint32_t size); /* rom stays where it is (flash) */
void v3_reset(void);
uint64_t v3_run(uint64_t ticks); /* runs at least ticks, returns the ticks run */
uint32_t v3_step(void); /* one instruction (or one 32 kHz step while stopped), its ticks */
void v3_set_keys(uint32_t mask); /* bit 0 left (A), 1 middle (B), 2 right (C) */
const uint8_t *v3_vram(int *mode); /* the LCD RAM (0x200 bytes); mode 0 off, 1 on, 2 all on */
uint32_t v3_tone_hz(void); /* square tone of the buzzer now, 0 = quiet */
uint8_t *v3_eeprom(void);
const uint8_t *v3_state_ee(const uint8_t *b); /* test21 */ /* 1 KB */
int v3_eeprom_dirty(int clear);
uint32_t v3_state_size(void);
void v3_state_save(uint8_t *b);
int v3_state_load(const uint8_t *b, uint32_t n);
void v3_ir_rx(const uint8_t *d, int n); /* infrared: bytes from the other toy */
int v3_ir_tx(uint8_t *out, int max); /* infrared: bytes this toy sent (taken out) */
uint8_t *v3_ram(void); /* 0x4C0 bytes from address 0x40 */
/* debug */
uint32_t v3_pc(void); void v3_regs(uint8_t r[5], uint8_t f[6]); uint32_t v3_bank(void);
#endif
/* v3cpu.h - Tamagotchi Connection V3 (Sunplus SPLB32, a 6502) in C, ported from BrickEmuPy (azya52, CC0):
* cores/SPLB32.py, peripherals/HT24LC08.py (EEPROM), direct_input.py (keys). Time in ticks of 1/128 cycle
* (4 MHz main clock, the 32768 Hz clock is exactly 15625 ticks), so the timers count as the Python floats did.
* Author: mameise */
#ifndef V3CPU_H
#define V3CPU_H
#include <stdint.h>
#define V3_CLOCK 4000000u
#define V3_TICKS 128u /* ticks per cycle */
#define V3_EE_SIZE 1024u
#define V3_DPRAM 0x200u
void v3_init(const uint8_t *rom, uint32_t size); /* rom stays where it is (flash) */
void v3_reset(void);
uint64_t v3_run(uint64_t ticks); /* runs at least ticks, returns the ticks run */
uint32_t v3_step(void); /* one instruction (or one 32 kHz step while stopped), its ticks */
void v3_set_keys(uint32_t mask); /* bit 0 left (A), 1 middle (B), 2 right (C) */
const uint8_t *v3_vram(int *mode); /* the LCD RAM (0x200 bytes); mode 0 off, 1 on, 2 all on */
uint32_t v3_tone_hz(void); /* square tone of the buzzer now, 0 = quiet */
uint8_t *v3_eeprom(void);
const uint8_t *v3_state_ee(const uint8_t *b); /* test21 */ /* 1 KB */
int v3_eeprom_dirty(int clear);
uint32_t v3_state_size(void);
void v3_state_save(uint8_t *b);
int v3_state_load(const uint8_t *b, uint32_t n);
void v3_ir_rx(const uint8_t *d, int n); /* infrared: bytes from the other toy */
int v3_ir_tx(uint8_t *out, int max); /* infrared: bytes this toy sent (taken out) */
uint8_t *v3_ram(void); /* 0x4C0 bytes from address 0x40 */
/* debug */
uint32_t v3_pc(void); void v3_regs(uint8_t r[5], uint8_t f[6]); uint32_t v3_bank(void);
#endif
+33 -33
View File
@@ -1,33 +1,33 @@
/* LCD RAM bit (byte * 8 + bit) of each pixel of the 32 x 30 dot matrix, from the face of BrickEmuPy (assets/Tamagotchi_v3.svg, CC0) */
static const uint16_t V3_LCD[30][32] = {
{128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159},
{256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287},
{384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415},
{512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530,531,532,533,534,535,536,537,538,539,540,541,542,543},
{640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671},
{768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799},
{896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927},
{1920,1921,1922,1923,1924,1925,1926,1927,1928,1929,1930,1931,1932,1933,1934,1935,1936,1937,1938,1939,1940,1941,1942,1943,1944,1945,1946,1947,1948,1949,1950,1951},
{1792,1793,1794,1795,1796,1797,1798,1799,1800,1801,1802,1803,1804,1805,1806,1807,1808,1809,1810,1811,1812,1813,1814,1815,1816,1817,1818,1819,1820,1821,1822,1823},
{1664,1665,1666,1667,1668,1669,1670,1671,1672,1673,1674,1675,1676,1677,1678,1679,1680,1681,1682,1683,1684,1685,1686,1687,1688,1689,1690,1691,1692,1693,1694,1695},
{1536,1537,1538,1539,1540,1541,1542,1543,1544,1545,1546,1547,1548,1549,1550,1551,1552,1553,1554,1555,1556,1557,1558,1559,1560,1561,1562,1563,1564,1565,1566,1567},
{1408,1409,1410,1411,1412,1413,1414,1415,1416,1417,1418,1419,1420,1421,1422,1423,1424,1425,1426,1427,1428,1429,1430,1431,1432,1433,1434,1435,1436,1437,1438,1439},
{1280,1281,1282,1283,1284,1285,1286,1287,1288,1289,1290,1291,1292,1293,1294,1295,1296,1297,1298,1299,1300,1301,1302,1303,1304,1305,1306,1307,1308,1309,1310,1311},
{1152,1153,1154,1155,1156,1157,1158,1159,1160,1161,1162,1163,1164,1165,1166,1167,1168,1169,1170,1171,1172,1173,1174,1175,1176,1177,1178,1179,1180,1181,1182,1183},
{1024,1025,1026,1027,1028,1029,1030,1031,1032,1033,1034,1035,1036,1037,1038,1039,1040,1041,1042,1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055},
{1087,1086,1085,1084,1083,1082,1081,1080,1079,1078,1077,1076,1075,1074,1073,1072,1071,1070,1069,1068,1067,1066,1065,1064,1063,1062,1061,1060,1059,1058,1057,1056},
{1215,1214,1213,1212,1211,1210,1209,1208,1207,1206,1205,1204,1203,1202,1201,1200,1199,1198,1197,1196,1195,1194,1193,1192,1191,1190,1189,1188,1187,1186,1185,1184},
{1343,1342,1341,1340,1339,1338,1337,1336,1335,1334,1333,1332,1331,1330,1329,1328,1327,1326,1325,1324,1323,1322,1321,1320,1319,1318,1317,1316,1315,1314,1313,1312},
{1471,1470,1469,1468,1467,1466,1465,1464,1463,1462,1461,1460,1459,1458,1457,1456,1455,1454,1453,1452,1451,1450,1449,1448,1447,1446,1445,1444,1443,1442,1441,1440},
{1599,1598,1597,1596,1595,1594,1593,1592,1591,1590,1589,1588,1587,1586,1585,1584,1583,1582,1581,1580,1579,1578,1577,1576,1575,1574,1573,1572,1571,1570,1569,1568},
{1727,1726,1725,1724,1723,1722,1721,1720,1719,1718,1717,1716,1715,1714,1713,1712,1711,1710,1709,1708,1707,1706,1705,1704,1703,1702,1701,1700,1699,1698,1697,1696},
{1855,1854,1853,1852,1851,1850,1849,1848,1847,1846,1845,1844,1843,1842,1841,1840,1839,1838,1837,1836,1835,1834,1833,1832,1831,1830,1829,1828,1827,1826,1825,1824},
{1983,1982,1981,1980,1979,1978,1977,1976,1975,1974,1973,1972,1971,1970,1969,1968,1967,1966,1965,1964,1963,1962,1961,1960,1959,1958,1957,1956,1955,1954,1953,1952},
{959,958,957,956,955,954,953,952,951,950,949,948,947,946,945,944,943,942,941,940,939,938,937,936,935,934,933,932,931,930,929,928},
{831,830,829,828,827,826,825,824,823,822,821,820,819,818,817,816,815,814,813,812,811,810,809,808,807,806,805,804,803,802,801,800},
{703,702,701,700,699,698,697,696,695,694,693,692,691,690,689,688,687,686,685,684,683,682,681,680,679,678,677,676,675,674,673,672},
{575,574,573,572,571,570,569,568,567,566,565,564,563,562,561,560,559,558,557,556,555,554,553,552,551,550,549,548,547,546,545,544},
{447,446,445,444,443,442,441,440,439,438,437,436,435,434,433,432,431,430,429,428,427,426,425,424,423,422,421,420,419,418,417,416},
{319,318,317,316,315,314,313,312,311,310,309,308,307,306,305,304,303,302,301,300,299,298,297,296,295,294,293,292,291,290,289,288},
{191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160}
};
/* LCD RAM bit (byte * 8 + bit) of each pixel of the 32 x 30 dot matrix, from the face of BrickEmuPy (assets/Tamagotchi_v3.svg, CC0) */
static const uint16_t V3_LCD[30][32] = {
{128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159},
{256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287},
{384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415},
{512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530,531,532,533,534,535,536,537,538,539,540,541,542,543},
{640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671},
{768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799},
{896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927},
{1920,1921,1922,1923,1924,1925,1926,1927,1928,1929,1930,1931,1932,1933,1934,1935,1936,1937,1938,1939,1940,1941,1942,1943,1944,1945,1946,1947,1948,1949,1950,1951},
{1792,1793,1794,1795,1796,1797,1798,1799,1800,1801,1802,1803,1804,1805,1806,1807,1808,1809,1810,1811,1812,1813,1814,1815,1816,1817,1818,1819,1820,1821,1822,1823},
{1664,1665,1666,1667,1668,1669,1670,1671,1672,1673,1674,1675,1676,1677,1678,1679,1680,1681,1682,1683,1684,1685,1686,1687,1688,1689,1690,1691,1692,1693,1694,1695},
{1536,1537,1538,1539,1540,1541,1542,1543,1544,1545,1546,1547,1548,1549,1550,1551,1552,1553,1554,1555,1556,1557,1558,1559,1560,1561,1562,1563,1564,1565,1566,1567},
{1408,1409,1410,1411,1412,1413,1414,1415,1416,1417,1418,1419,1420,1421,1422,1423,1424,1425,1426,1427,1428,1429,1430,1431,1432,1433,1434,1435,1436,1437,1438,1439},
{1280,1281,1282,1283,1284,1285,1286,1287,1288,1289,1290,1291,1292,1293,1294,1295,1296,1297,1298,1299,1300,1301,1302,1303,1304,1305,1306,1307,1308,1309,1310,1311},
{1152,1153,1154,1155,1156,1157,1158,1159,1160,1161,1162,1163,1164,1165,1166,1167,1168,1169,1170,1171,1172,1173,1174,1175,1176,1177,1178,1179,1180,1181,1182,1183},
{1024,1025,1026,1027,1028,1029,1030,1031,1032,1033,1034,1035,1036,1037,1038,1039,1040,1041,1042,1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055},
{1087,1086,1085,1084,1083,1082,1081,1080,1079,1078,1077,1076,1075,1074,1073,1072,1071,1070,1069,1068,1067,1066,1065,1064,1063,1062,1061,1060,1059,1058,1057,1056},
{1215,1214,1213,1212,1211,1210,1209,1208,1207,1206,1205,1204,1203,1202,1201,1200,1199,1198,1197,1196,1195,1194,1193,1192,1191,1190,1189,1188,1187,1186,1185,1184},
{1343,1342,1341,1340,1339,1338,1337,1336,1335,1334,1333,1332,1331,1330,1329,1328,1327,1326,1325,1324,1323,1322,1321,1320,1319,1318,1317,1316,1315,1314,1313,1312},
{1471,1470,1469,1468,1467,1466,1465,1464,1463,1462,1461,1460,1459,1458,1457,1456,1455,1454,1453,1452,1451,1450,1449,1448,1447,1446,1445,1444,1443,1442,1441,1440},
{1599,1598,1597,1596,1595,1594,1593,1592,1591,1590,1589,1588,1587,1586,1585,1584,1583,1582,1581,1580,1579,1578,1577,1576,1575,1574,1573,1572,1571,1570,1569,1568},
{1727,1726,1725,1724,1723,1722,1721,1720,1719,1718,1717,1716,1715,1714,1713,1712,1711,1710,1709,1708,1707,1706,1705,1704,1703,1702,1701,1700,1699,1698,1697,1696},
{1855,1854,1853,1852,1851,1850,1849,1848,1847,1846,1845,1844,1843,1842,1841,1840,1839,1838,1837,1836,1835,1834,1833,1832,1831,1830,1829,1828,1827,1826,1825,1824},
{1983,1982,1981,1980,1979,1978,1977,1976,1975,1974,1973,1972,1971,1970,1969,1968,1967,1966,1965,1964,1963,1962,1961,1960,1959,1958,1957,1956,1955,1954,1953,1952},
{959,958,957,956,955,954,953,952,951,950,949,948,947,946,945,944,943,942,941,940,939,938,937,936,935,934,933,932,931,930,929,928},
{831,830,829,828,827,826,825,824,823,822,821,820,819,818,817,816,815,814,813,812,811,810,809,808,807,806,805,804,803,802,801,800},
{703,702,701,700,699,698,697,696,695,694,693,692,691,690,689,688,687,686,685,684,683,682,681,680,679,678,677,676,675,674,673,672},
{575,574,573,572,571,570,569,568,567,566,565,564,563,562,561,560,559,558,557,556,555,554,553,552,551,550,549,548,547,546,545,544},
{447,446,445,444,443,442,441,440,439,438,437,436,435,434,433,432,431,430,429,428,427,426,425,424,423,422,421,420,419,418,417,416},
{319,318,317,316,315,314,313,312,311,310,309,308,307,306,305,304,303,302,301,300,299,298,297,296,295,294,293,292,291,290,289,288},
{191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160}
};
+1580 -1580
View File
File diff suppressed because it is too large. Load diff
+179 -179
View File
@@ -1,179 +1,179 @@
# V3 app (v3.bin) in Unicorn, as devt7.py: hardware calls caught, the screen into a picture.
# into a picture, the SysTick is advanced every RT instructions (96 MHz Cortex-M3 ~ RT per ms).
# python3 devv3.py <v3.elf> <image with v3.bin at APPOFF> <ms> [KEYS="t:mask,..."] [SHOTS="t,t,..."] [OUT=dir]
import sys, struct, subprocess, os
from unicorn import *
from unicorn.arm_const import *
elf, img, total_ms = sys.argv[1], sys.argv[2], int(sys.argv[3])
RT = int(os.environ.get('RT', '64000')) # instructions per millisecond (96 MHz, ~1.5 cycles each)
out = os.environ.get('OUT', '.')
syms = {}
for l in subprocess.run(['llvm-nm', elf], capture_output=True, text=True).stdout.splitlines():
p = l.split()
if len(p) == 3: syms[p[2]] = int(p[0], 16)
fl = bytearray(open(img, 'rb').read())
u = Uc(UC_ARCH_ARM, UC_MODE_THUMB | UC_MODE_MCLASS)
u.ctl_set_cpu_model(UC_CPU_ARM_CORTEX_M4)
APPOFF = int(os.environ.get('APPOFF', '0xA0000'), 16) # program image (1.6.0: more programs elsewhere)
u.mem_map(0, 0x10000); u.mem_write(0, bytes(fl[APPOFF:APPOFF + 0x10000]))
u.mem_map(0x18000000, 0x20000); u.mem_map(0x20000000, 0x1000)
u.mem_map(0x10000000, 0x100000)
u.mem_map(0x60000000, 0x1000000)
if len(fl) < 0x1000000: fl += b'\xff' * (0x1000000 - len(fl))
# FLASHGATE=1: as on the device after the reset, the window shows nothing useful until hw_init
if os.environ.get('FLASHGATE'): u.mem_write(0x60000000, b'\x40' * 0x1000000)
else: u.mem_write(0x60000000, bytes(fl))
u.mem_map(0x40000000, 0x100000); u.mem_map(0x45000000, 0x1000); u.mem_map(0xE000E000, 0x1000); u.mem_map(0xE0001000, 0x1000) # DWT (CYCCNT reads 0 here)
u.mem_write(0x4000B014, struct.pack('<I', 0x60)) # 1.6.9 T7_LINK: UART1 LSR, sender empty (nothing received)
cfg = APPOFF + 0x140
rom_off = struct.unpack_from('<I', fl, cfg + 8)[0]
u.mem_write(0x10000000, bytes(fl[rom_off:rom_off + 0x100000]))
fb = [[0] * 128 for _ in range(128)]
rect = {'x0': 0, 'x1': 0, 'y': 0, 'x': 0}
KS = []
for p in os.environ.get('KEYS', '').split(','):
if ':' in p: t, m = p.split(':'); KS.append((float(t), int(m)))
SH = [float(x) for x in os.environ.get('SHOTS', '').split(',') if x]
state = {'ms': 0, 'tone': [], 'left': 0, 'flashw': 0}
def keys():
m = 0
for t, v in KS:
if state['ms'] / 1000.0 >= t: m = v
return m
def ret(uc, v=None):
if v is not None: uc.reg_write(UC_ARM_REG_R0, v)
uc.reg_write(UC_ARM_REG_PC, uc.reg_read(UC_ARM_REG_LR) | 1)
def h_begin(uc, *a):
x0, y0, x1, y1 = (uc.reg_read(r) for r in (UC_ARM_REG_R0, UC_ARM_REG_R1, UC_ARM_REG_R2, UC_ARM_REG_R3))
rect.update(x0=x0, x1=x1, y=y0, x=x0); ret(uc)
def h_row(uc, *a):
p, n = uc.reg_read(UC_ARM_REG_R0), uc.reg_read(UC_ARM_REG_R1)
d = struct.unpack('<%dH' % n, uc.mem_read(p, 2 * n))
for v in d:
if rect['y'] < 128 and rect['x'] < 128: fb[rect['y']][rect['x']] = v
rect['x'] += 1
if rect['x'] >= rect['x1']: rect['x'] = rect['x0']; rect['y'] += 1
ret(uc)
def h_tone(uc, *a): state['tone'].append((state['ms'], uc.reg_read(UC_ARM_REG_R0))); ret(uc)
def h_btn(uc, *a): ret(uc, keys())
def h_prog(uc, *a):
ad, src = uc.reg_read(UC_ARM_REG_R0), uc.reg_read(UC_ARM_REG_R1)
d = bytes(uc.mem_read(src, 0x1000)); a = ad & 0xFFFFFF
uc.mem_write(0x60000000 + a, d); fl[a:a + 0x1000] = d; state['flashw'] += 1; ret(uc)
def h_erase(uc, *a):
a = uc.reg_read(UC_ARM_REG_R0) & 0xFFFFFF
uc.mem_write(0x60000000 + a, b'\xff' * 0x1000); fl[a:a + 0x1000] = b'\xff' * 0x1000; ret(uc)
def h_back(uc, *a): state['back'] = 1; uc.emu_stop()
def h_nop(uc, *a): ret(uc)
def h_init(uc, *a):
if os.environ.get('FLASHGATE') and not state.get('gate'): uc.mem_write(0x60000000, bytes(fl)); state['gate'] = 1
ret(uc)
H = {'hw_px_begin': h_begin, 'hw_px_row': h_row, 'hw_px_end': h_nop, 'hw_tone': h_tone, 'hw_buttons': h_btn,
'flash_prog4k': h_prog, 'flash_erase4k': h_erase, 'hw_back_to_paradise': h_back, 'hw_init': h_init,
'hw_backlight': h_nop, 'hw_bl_pwm_off': h_nop, 'hw_wdt_feed': h_nop}
for n, f in H.items():
a = syms[n] & ~1
u.hook_add(UC_HOOK_CODE, (lambda f: lambda uc, ad, sz, ud: f(uc))(f), begin=a, end=a)
def bad(uc, access, addr, size, value, ud):
print('unmapped', access, hex(addr), 'pc', hex(uc.reg_read(UC_ARM_REG_PC))); return False
u.hook_add(UC_HOOK_MEM_UNMAPPED, bad)
# 1.6.9 test22 CLK_ECO: the AHB prescaler at 0x45000010 slows the core: RT >> sh instructions per ms
state['sh'] = 0; state['shms'] = [0, 0, 0, 0]
def _ahb(uc, acc, a, sz, v, ud):
state['sh'] = v & 7
if os.environ.get('ECOLOG'): print('AHB', v & 7, 'at ms', state['ms'])
u.hook_add(UC_HOOK_MEM_WRITE, _ahb, begin=0x45000010, end=0x45000013)
if os.environ.get('MEMW'):
_mw = int(os.environ['MEMW'], 16)
u.hook_add(UC_HOOK_MEM_WRITE, lambda uc, acc, a, sz, v, ud: print('W %08x = %08x at ms %d' % (a, v, state['ms'])), begin=_mw, end=_mw + 0x3F)
# 1.6.13 test12 (V3_PW, as devt7/devemu): clock source (0x4500000C), deep sleep (PMU_CTRL bit 1, then WFI): the time
# runs on to the next RTC second or a key change; the load: the ms in NCH parts, the part with the main loop's wfi
u.mem_write(0x45000008, struct.pack('<I', 0x55)); u.mem_write(0x45000200, struct.pack('<I', 0x80)); u.mem_write(0x4500000C, struct.pack('<I', 0x44))
state['ihrc'] = 0; state['pw'] = {'full': 0, 'ihrc': 0, 'sleep': 0}; state['busy'] = 0; state['busyn'] = 0
def _clk(uc, acc, a, sz, v, ud): state['ihrc'] = (v & 7) == 0; state['clkv'] = v & 7
u.hook_add(UC_HOOK_MEM_WRITE, _clk, begin=0x4500000C, end=0x4500000F)
def _clkrd(uc, acc, a, sz, v, ud):
if 'clkv' in state: c = state['clkv']; uc.mem_write(0x4500000C, struct.pack('<I', c | (c << 4)))
u.hook_add(UC_HOOK_MEM_READ, _clkrd, begin=0x4500000C, end=0x4500000F)
def _pmu(uc, acc, a, sz, v, ud):
if v & 2 and not state.get('dsleep'):
t = state['ms']
while True:
t += 1
if t % 1000 == 0 or t >= total_ms or any(t / 1000.0 >= kt > state['ms'] / 1000.0 for kt, _ in KS): break
state['dsleep'] = t
if t % 1000 == 0 and 'g_pw_rtcw' in syms: uc.mem_write(syms['g_pw_rtcw'], b'\x01')
uc.mem_write(0x45000304, struct.pack('<I', int(os.environ.get('RTC0', '5000')) + t // 1000))
u.hook_add(UC_HOOK_MEM_WRITE, _pmu, begin=0x45000300, end=0x45000303)
NCH = int(os.environ.get('NCH', '8'))
_code = bytes(fl[APPOFF:APPOFF + 0x10000]); _end = syms.get('__prog_end', 0x10000)
def _wfi(uc, a, sz, ud): state['waited'] = 1; uc.emu_stop()
for _a in [i for i in range(0, _end, 2) if _code[i:i + 2] == b'\x30\xbf']: u.hook_add(UC_HOOK_CODE, _wfi, begin=_a, end=_a)
PROF = {} if os.environ.get('PROF') else None
def shot(name):
from PIL import Image
im = Image.new('RGB', (128, 128))
for y in range(128):
for x in range(128):
v = fb[y][x]; im.putpixel((x, y), (((v >> 11) & 31) << 3, ((v >> 5) & 63) << 2, (v & 31) << 3))
im.save(os.path.join(out, name))
if os.environ.get('PMB'): # test: the Paradise's time in the mailbox (seconds since 1970, local)
_t = int(os.environ['PMB']); _c = int(os.environ.get('RTC0', '5000'))
u.mem_write(0x20000FC0, struct.pack('<IIII', 0x4B4C4350, _t, _c, (~_t) & 0xFFFFFFFF))
sp, rst = struct.unpack('<II', bytes(u.mem_read(0, 8)))
u.reg_write(UC_ARM_REG_SP, sp)
pc = rst; gms = syms['g_ms']; rtc0 = int(os.environ.get('RTC0', '5000'))
emu_ms_sym = None
si = 0
while state['ms'] < total_ms and 'back' not in state:
u.mem_write(0x45000304, struct.pack('<I', rtc0 + state['ms'] // 1000))
state['waited'] = 0; _sh = 3 if state['ihrc'] else state['sh']; _n = max(1, RT >> _sh); _ch = max(1, _n // NCH); _k = 0; _err = 0
while _k < NCH and not state['waited'] and 'back' not in state and not state.get('dsleep'):
try: u.emu_start(pc | 1, 0xFFFFFFF0, count=_ch)
except UcError as ex:
print('error', ex, hex(u.reg_read(UC_ARM_REG_PC))); _err = 1; break
pc = u.reg_read(UC_ARM_REG_PC); _k += 1
if PROF is not None and not state['waited']: PROF[pc] = PROF.get(pc, 0) + 1
if _err: break
state['shms'][min(_sh, 3)] += 1; state['pw']['ihrc' if state['ihrc'] else 'full'] += 1; state['busy'] += _k; state['busyn'] += 1
if state['waited']: pc = (pc + 2) & ~1
_ds = state.pop('dsleep', 0)
if _ds:
state['pw']['sleep'] += _ds - state['ms']; state['ms'] = _ds
u.mem_write(0x45000304, struct.pack('<I', rtc0 + state['ms'] // 1000)); continue
state['ms'] += 1
v = struct.unpack('<I', u.mem_read(gms, 4))[0]; u.mem_write(gms, struct.pack('<I', v + 1))
# 1.6.9 test13 T7_KEYQ: the GPIO as the keys say (pressed = 0) and the tick's press counter
km = keys(); u.mem_write(0x40018000, struct.pack('<I', 0xFFFFFFFF & ~(((km & 1) << 9) | ((km & 2) << 10) | ((km & 4) << 8) | ((km & 8) << 5))))
if 'g_kin' in syms:
e = km & ~state.get('kprev', 0) & 7; state['kprev'] = km
u.mem_write(syms['g_kprev'], bytes([km & 7]))
for i in range(3):
if e & (1 << i): a = syms['g_kin'] + i; u.mem_write(a, bytes([(u.mem_read(a, 1)[0] + 1) & 0xFF]))
for pk in os.environ.get('POKE', '').split(','): # test: 'ms:symbol+off:byte' written once
if pk.count(':') == 2 and int(pk.split(':')[0]) == state['ms']:
_t, _a, _v = pk.split(':'); _n, _o = (_a.split('+') + ['0'])[:2]; u.mem_write(syms[_n] + int(_o, 0), bytes([int(_v, 0)]))
if os.environ.get('DROPLOG') and 'g_v3st' in syms:
_d = struct.unpack('<I', u.mem_read(syms['g_v3st'] + 4, 4))[0]
if _d != state.get('drop', 0): print('drop', _d - state.get('drop', 0), 'at ms', state['ms'], 'sh', state['sh']); state['drop'] = _d
if si < len(SH) and state['ms'] >= SH[si] * 1000: shot('shot_%02d.png' % si); si += 1
st = struct.unpack('<2I', bytes(u.mem_read(syms['g_v3st'], 8))) if 'g_v3st' in syms else (0, 0) # V1Emu has no counters
print('emulated ms', st[0], 'dropped ms', st[1])
(os.environ.get('TONES') and print('tones:', state['tone']));print('ms', state['ms'], 'tones', len(state['tone']), 'flash writes', state['flashw'], 'back' if 'back' in state else '', 'pc', hex(pc))
print('ms per clock step (96, 48, 24, 12 MHz):', state['shms'])
print('power ms: full', state['pw']['full'], 'ihrc', state['pw']['ihrc'], 'deep sleep', state['pw']['sleep'])
print('load %.1f %% of the CPU (awake ms)' % (100.0 * state['busy'] / max(1, state['busyn'] * NCH)))
if PROF:
import bisect
fs = sorted(((v, k) for k, v in syms.items() if v < 0x20000000), key=lambda t: t[0]); fa = [a for a, _ in fs]; agg = {}
for a, n in PROF.items():
i = bisect.bisect_right(fa, a & ~1) - 1; nm = fs[i][1] if i >= 0 else '?'
if a >= 0x18000000: nm = 'SRAM code'
agg[nm] = agg.get(nm, 0) + n
tot = sum(agg.values()); print('profile:', ', '.join('%s %.1f%%' % (k, 100.0 * v / tot) for k, v in sorted(agg.items(), key=lambda kv: -kv[1])[:16]))
open(os.path.join(out, 'flash_after.bin'), 'wb').write(fl)
shot('shot_end.png')
for _d in os.environ.get('DUMP', '').split(','): # test: 'symbol+off:len' printed at the end
if ':' in _d:
_a, _l = _d.split(':'); _n, _o = (_a.split('+') + ['0'])[:2]
print('DUMP', _d, bytes(u.mem_read(syms[_n] + int(_o, 0), int(_l, 0))).hex())
# V3 app (v3.bin) in Unicorn, as devt7.py: hardware calls caught, the screen into a picture.
# into a picture, the SysTick is advanced every RT instructions (96 MHz Cortex-M3 ~ RT per ms).
# python3 devv3.py <v3.elf> <image with v3.bin at APPOFF> <ms> [KEYS="t:mask,..."] [SHOTS="t,t,..."] [OUT=dir]
import sys, struct, subprocess, os
from unicorn import *
from unicorn.arm_const import *
elf, img, total_ms = sys.argv[1], sys.argv[2], int(sys.argv[3])
RT = int(os.environ.get('RT', '64000')) # instructions per millisecond (96 MHz, ~1.5 cycles each)
out = os.environ.get('OUT', '.')
syms = {}
for l in subprocess.run(['llvm-nm', elf], capture_output=True, text=True).stdout.splitlines():
p = l.split()
if len(p) == 3: syms[p[2]] = int(p[0], 16)
fl = bytearray(open(img, 'rb').read())
u = Uc(UC_ARCH_ARM, UC_MODE_THUMB | UC_MODE_MCLASS)
u.ctl_set_cpu_model(UC_CPU_ARM_CORTEX_M4)
APPOFF = int(os.environ.get('APPOFF', '0xA0000'), 16) # program image (1.6.0: more programs elsewhere)
u.mem_map(0, 0x10000); u.mem_write(0, bytes(fl[APPOFF:APPOFF + 0x10000]))
u.mem_map(0x18000000, 0x20000); u.mem_map(0x20000000, 0x1000)
u.mem_map(0x10000000, 0x100000)
u.mem_map(0x60000000, 0x1000000)
if len(fl) < 0x1000000: fl += b'\xff' * (0x1000000 - len(fl))
# FLASHGATE=1: as on the device after the reset, the window shows nothing useful until hw_init
if os.environ.get('FLASHGATE'): u.mem_write(0x60000000, b'\x40' * 0x1000000)
else: u.mem_write(0x60000000, bytes(fl))
u.mem_map(0x40000000, 0x100000); u.mem_map(0x45000000, 0x1000); u.mem_map(0xE000E000, 0x1000); u.mem_map(0xE0001000, 0x1000) # DWT (CYCCNT reads 0 here)
u.mem_write(0x4000B014, struct.pack('<I', 0x60)) # 1.6.9 T7_LINK: UART1 LSR, sender empty (nothing received)
cfg = APPOFF + 0x140
rom_off = struct.unpack_from('<I', fl, cfg + 8)[0]
u.mem_write(0x10000000, bytes(fl[rom_off:rom_off + 0x100000]))
fb = [[0] * 128 for _ in range(128)]
rect = {'x0': 0, 'x1': 0, 'y': 0, 'x': 0}
KS = []
for p in os.environ.get('KEYS', '').split(','):
if ':' in p: t, m = p.split(':'); KS.append((float(t), int(m)))
SH = [float(x) for x in os.environ.get('SHOTS', '').split(',') if x]
state = {'ms': 0, 'tone': [], 'left': 0, 'flashw': 0}
def keys():
m = 0
for t, v in KS:
if state['ms'] / 1000.0 >= t: m = v
return m
def ret(uc, v=None):
if v is not None: uc.reg_write(UC_ARM_REG_R0, v)
uc.reg_write(UC_ARM_REG_PC, uc.reg_read(UC_ARM_REG_LR) | 1)
def h_begin(uc, *a):
x0, y0, x1, y1 = (uc.reg_read(r) for r in (UC_ARM_REG_R0, UC_ARM_REG_R1, UC_ARM_REG_R2, UC_ARM_REG_R3))
rect.update(x0=x0, x1=x1, y=y0, x=x0); ret(uc)
def h_row(uc, *a):
p, n = uc.reg_read(UC_ARM_REG_R0), uc.reg_read(UC_ARM_REG_R1)
d = struct.unpack('<%dH' % n, uc.mem_read(p, 2 * n))
for v in d:
if rect['y'] < 128 and rect['x'] < 128: fb[rect['y']][rect['x']] = v
rect['x'] += 1
if rect['x'] >= rect['x1']: rect['x'] = rect['x0']; rect['y'] += 1
ret(uc)
def h_tone(uc, *a): state['tone'].append((state['ms'], uc.reg_read(UC_ARM_REG_R0))); ret(uc)
def h_btn(uc, *a): ret(uc, keys())
def h_prog(uc, *a):
ad, src = uc.reg_read(UC_ARM_REG_R0), uc.reg_read(UC_ARM_REG_R1)
d = bytes(uc.mem_read(src, 0x1000)); a = ad & 0xFFFFFF
uc.mem_write(0x60000000 + a, d); fl[a:a + 0x1000] = d; state['flashw'] += 1; ret(uc)
def h_erase(uc, *a):
a = uc.reg_read(UC_ARM_REG_R0) & 0xFFFFFF
uc.mem_write(0x60000000 + a, b'\xff' * 0x1000); fl[a:a + 0x1000] = b'\xff' * 0x1000; ret(uc)
def h_back(uc, *a): state['back'] = 1; uc.emu_stop()
def h_nop(uc, *a): ret(uc)
def h_init(uc, *a):
if os.environ.get('FLASHGATE') and not state.get('gate'): uc.mem_write(0x60000000, bytes(fl)); state['gate'] = 1
ret(uc)
H = {'hw_px_begin': h_begin, 'hw_px_row': h_row, 'hw_px_end': h_nop, 'hw_tone': h_tone, 'hw_buttons': h_btn,
'flash_prog4k': h_prog, 'flash_erase4k': h_erase, 'hw_back_to_paradise': h_back, 'hw_init': h_init,
'hw_backlight': h_nop, 'hw_bl_pwm_off': h_nop, 'hw_wdt_feed': h_nop}
for n, f in H.items():
a = syms[n] & ~1
u.hook_add(UC_HOOK_CODE, (lambda f: lambda uc, ad, sz, ud: f(uc))(f), begin=a, end=a)
def bad(uc, access, addr, size, value, ud):
print('unmapped', access, hex(addr), 'pc', hex(uc.reg_read(UC_ARM_REG_PC))); return False
u.hook_add(UC_HOOK_MEM_UNMAPPED, bad)
# 1.6.9 test22 CLK_ECO: the AHB prescaler at 0x45000010 slows the core: RT >> sh instructions per ms
state['sh'] = 0; state['shms'] = [0, 0, 0, 0]
def _ahb(uc, acc, a, sz, v, ud):
state['sh'] = v & 7
if os.environ.get('ECOLOG'): print('AHB', v & 7, 'at ms', state['ms'])
u.hook_add(UC_HOOK_MEM_WRITE, _ahb, begin=0x45000010, end=0x45000013)
if os.environ.get('MEMW'):
_mw = int(os.environ['MEMW'], 16)
u.hook_add(UC_HOOK_MEM_WRITE, lambda uc, acc, a, sz, v, ud: print('W %08x = %08x at ms %d' % (a, v, state['ms'])), begin=_mw, end=_mw + 0x3F)
# 1.6.13 test12 (V3_PW, as devt7/devemu): clock source (0x4500000C), deep sleep (PMU_CTRL bit 1, then WFI): the time
# runs on to the next RTC second or a key change; the load: the ms in NCH parts, the part with the main loop's wfi
u.mem_write(0x45000008, struct.pack('<I', 0x55)); u.mem_write(0x45000200, struct.pack('<I', 0x80)); u.mem_write(0x4500000C, struct.pack('<I', 0x44))
state['ihrc'] = 0; state['pw'] = {'full': 0, 'ihrc': 0, 'sleep': 0}; state['busy'] = 0; state['busyn'] = 0
def _clk(uc, acc, a, sz, v, ud): state['ihrc'] = (v & 7) == 0; state['clkv'] = v & 7
u.hook_add(UC_HOOK_MEM_WRITE, _clk, begin=0x4500000C, end=0x4500000F)
def _clkrd(uc, acc, a, sz, v, ud):
if 'clkv' in state: c = state['clkv']; uc.mem_write(0x4500000C, struct.pack('<I', c | (c << 4)))
u.hook_add(UC_HOOK_MEM_READ, _clkrd, begin=0x4500000C, end=0x4500000F)
def _pmu(uc, acc, a, sz, v, ud):
if v & 2 and not state.get('dsleep'):
t = state['ms']
while True:
t += 1
if t % 1000 == 0 or t >= total_ms or any(t / 1000.0 >= kt > state['ms'] / 1000.0 for kt, _ in KS): break
state['dsleep'] = t
if t % 1000 == 0 and 'g_pw_rtcw' in syms: uc.mem_write(syms['g_pw_rtcw'], b'\x01')
uc.mem_write(0x45000304, struct.pack('<I', int(os.environ.get('RTC0', '5000')) + t // 1000))
u.hook_add(UC_HOOK_MEM_WRITE, _pmu, begin=0x45000300, end=0x45000303)
NCH = int(os.environ.get('NCH', '8'))
_code = bytes(fl[APPOFF:APPOFF + 0x10000]); _end = syms.get('__prog_end', 0x10000)
def _wfi(uc, a, sz, ud): state['waited'] = 1; uc.emu_stop()
for _a in [i for i in range(0, _end, 2) if _code[i:i + 2] == b'\x30\xbf']: u.hook_add(UC_HOOK_CODE, _wfi, begin=_a, end=_a)
PROF = {} if os.environ.get('PROF') else None
def shot(name):
from PIL import Image
im = Image.new('RGB', (128, 128))
for y in range(128):
for x in range(128):
v = fb[y][x]; im.putpixel((x, y), (((v >> 11) & 31) << 3, ((v >> 5) & 63) << 2, (v & 31) << 3))
im.save(os.path.join(out, name))
if os.environ.get('PMB'): # test: the Paradise's time in the mailbox (seconds since 1970, local)
_t = int(os.environ['PMB']); _c = int(os.environ.get('RTC0', '5000'))
u.mem_write(0x20000FC0, struct.pack('<IIII', 0x4B4C4350, _t, _c, (~_t) & 0xFFFFFFFF))
sp, rst = struct.unpack('<II', bytes(u.mem_read(0, 8)))
u.reg_write(UC_ARM_REG_SP, sp)
pc = rst; gms = syms['g_ms']; rtc0 = int(os.environ.get('RTC0', '5000'))
emu_ms_sym = None
si = 0
while state['ms'] < total_ms and 'back' not in state:
u.mem_write(0x45000304, struct.pack('<I', rtc0 + state['ms'] // 1000))
state['waited'] = 0; _sh = 3 if state['ihrc'] else state['sh']; _n = max(1, RT >> _sh); _ch = max(1, _n // NCH); _k = 0; _err = 0
while _k < NCH and not state['waited'] and 'back' not in state and not state.get('dsleep'):
try: u.emu_start(pc | 1, 0xFFFFFFF0, count=_ch)
except UcError as ex:
print('error', ex, hex(u.reg_read(UC_ARM_REG_PC))); _err = 1; break
pc = u.reg_read(UC_ARM_REG_PC); _k += 1
if PROF is not None and not state['waited']: PROF[pc] = PROF.get(pc, 0) + 1
if _err: break
state['shms'][min(_sh, 3)] += 1; state['pw']['ihrc' if state['ihrc'] else 'full'] += 1; state['busy'] += _k; state['busyn'] += 1
if state['waited']: pc = (pc + 2) & ~1
_ds = state.pop('dsleep', 0)
if _ds:
state['pw']['sleep'] += _ds - state['ms']; state['ms'] = _ds
u.mem_write(0x45000304, struct.pack('<I', rtc0 + state['ms'] // 1000)); continue
state['ms'] += 1
v = struct.unpack('<I', u.mem_read(gms, 4))[0]; u.mem_write(gms, struct.pack('<I', v + 1))
# 1.6.9 test13 T7_KEYQ: the GPIO as the keys say (pressed = 0) and the tick's press counter
km = keys(); u.mem_write(0x40018000, struct.pack('<I', 0xFFFFFFFF & ~(((km & 1) << 9) | ((km & 2) << 10) | ((km & 4) << 8) | ((km & 8) << 5))))
if 'g_kin' in syms:
e = km & ~state.get('kprev', 0) & 7; state['kprev'] = km
u.mem_write(syms['g_kprev'], bytes([km & 7]))
for i in range(3):
if e & (1 << i): a = syms['g_kin'] + i; u.mem_write(a, bytes([(u.mem_read(a, 1)[0] + 1) & 0xFF]))
for pk in os.environ.get('POKE', '').split(','): # test: 'ms:symbol+off:byte' written once
if pk.count(':') == 2 and int(pk.split(':')[0]) == state['ms']:
_t, _a, _v = pk.split(':'); _n, _o = (_a.split('+') + ['0'])[:2]; u.mem_write(syms[_n] + int(_o, 0), bytes([int(_v, 0)]))
if os.environ.get('DROPLOG') and 'g_v3st' in syms:
_d = struct.unpack('<I', u.mem_read(syms['g_v3st'] + 4, 4))[0]
if _d != state.get('drop', 0): print('drop', _d - state.get('drop', 0), 'at ms', state['ms'], 'sh', state['sh']); state['drop'] = _d
if si < len(SH) and state['ms'] >= SH[si] * 1000: shot('shot_%02d.png' % si); si += 1
st = struct.unpack('<2I', bytes(u.mem_read(syms['g_v3st'], 8))) if 'g_v3st' in syms else (0, 0) # V1Emu has no counters
print('emulated ms', st[0], 'dropped ms', st[1])
(os.environ.get('TONES') and print('tones:', state['tone']));print('ms', state['ms'], 'tones', len(state['tone']), 'flash writes', state['flashw'], 'back' if 'back' in state else '', 'pc', hex(pc))
print('ms per clock step (96, 48, 24, 12 MHz):', state['shms'])
print('power ms: full', state['pw']['full'], 'ihrc', state['pw']['ihrc'], 'deep sleep', state['pw']['sleep'])
print('load %.1f %% of the CPU (awake ms)' % (100.0 * state['busy'] / max(1, state['busyn'] * NCH)))
if PROF:
import bisect
fs = sorted(((v, k) for k, v in syms.items() if v < 0x20000000), key=lambda t: t[0]); fa = [a for a, _ in fs]; agg = {}
for a, n in PROF.items():
i = bisect.bisect_right(fa, a & ~1) - 1; nm = fs[i][1] if i >= 0 else '?'
if a >= 0x18000000: nm = 'SRAM code'
agg[nm] = agg.get(nm, 0) + n
tot = sum(agg.values()); print('profile:', ', '.join('%s %.1f%%' % (k, 100.0 * v / tot) for k, v in sorted(agg.items(), key=lambda kv: -kv[1])[:16]))
open(os.path.join(out, 'flash_after.bin'), 'wb').write(fl)
shot('shot_end.png')
for _d in os.environ.get('DUMP', '').split(','): # test: 'symbol+off:len' printed at the end
if ':' in _d:
_a, _l = _d.split(':'); _n, _o = (_a.split('+') + ['0'])[:2]
print('DUMP', _d, bytes(u.mem_read(syms[_n] + int(_o, 0), int(_l, 0))).hex())
+8 -8
View File
@@ -1,8 +1,8 @@
# test image: v3.bin at 0xA0000 with its settings block, the ROM at 0x200000, the saves at 0x300000 (16 KB)
import sys, struct
app, rom, out = sys.argv[1], sys.argv[2], sys.argv[3]
fl = bytearray(b'\xff' * 0x400000)
a = open(app, 'rb').read(); r = open(rom, 'rb').read()
fl[0xA0000:0xA0000 + len(a)] = a; fl[0x200000:0x200000 + len(r)] = r
struct.pack_into('<6I16s', fl, 0xA0000 + 0x140, 0x31584D50, 1, 0x200000, len(r), 0x300000, int(sys.argv[4]) if len(sys.argv) > 4 else 0, b'TAMA V3')
open(out, 'wb').write(fl)
# test image: v3.bin at 0xA0000 with its settings block, the ROM at 0x200000, the saves at 0x300000 (16 KB)
import sys, struct
app, rom, out = sys.argv[1], sys.argv[2], sys.argv[3]
fl = bytearray(b'\xff' * 0x400000)
a = open(app, 'rb').read(); r = open(rom, 'rb').read()
fl[0xA0000:0xA0000 + len(a)] = a; fl[0x200000:0x200000 + len(r)] = r
struct.pack_into('<6I16s', fl, 0xA0000 + 0x140, 0x31584D50, 1, 0x200000, len(r), 0x300000, int(sys.argv[4]) if len(sys.argv) > 4 else 0, b'TAMA V3')
open(out, 'wb').write(fl)
+14 -14
View File
@@ -1,14 +1,14 @@
# per-function instruction profile of v3.bin in Unicorn (devv3 setup), counted from FROM ms on
import sys, os, collections, bisect
os.environ.setdefault('SHOTS', '')
FROM = int(os.environ.get('FROM', '0'))
src = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'devv3.py')).read()
src = src.replace("while state['ms'] < total_ms", "cnt=collections.Counter()\ndef hc(uc,a,s,d):\n if state['ms']>=FROM: cnt[a]+=1\nu.hook_add(UC_HOOK_CODE,hc,begin=0,end=0xffff)\nwhile state['ms'] < total_ms")
exec(compile(src, 'devv3', 'exec'))
fn = sorted((v, k) for k, v in syms.items() if v < 0x10000)
keys = [a for a, _ in fn]; per = collections.Counter()
for a, c in cnt.items():
i = bisect.bisect_right(keys, a) - 1; per[fn[i][1] if i >= 0 else '?'] += c
tot = sum(per.values())
for n, c in per.most_common(25): print('%-28s %6.2f%%' % (n, 100 * c / tot))
print('ARM instr total', tot, 'per ms', tot / max(1, total_ms - FROM))
# per-function instruction profile of v3.bin in Unicorn (devv3 setup), counted from FROM ms on
import sys, os, collections, bisect
os.environ.setdefault('SHOTS', '')
FROM = int(os.environ.get('FROM', '0'))
src = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'devv3.py')).read()
src = src.replace("while state['ms'] < total_ms", "cnt=collections.Counter()\ndef hc(uc,a,s,d):\n if state['ms']>=FROM: cnt[a]+=1\nu.hook_add(UC_HOOK_CODE,hc,begin=0,end=0xffff)\nwhile state['ms'] < total_ms")
exec(compile(src, 'devv3', 'exec'))
fn = sorted((v, k) for k, v in syms.items() if v < 0x10000)
keys = [a for a, _ in fn]; per = collections.Counter()
for a, c in cnt.items():
i = bisect.bisect_right(keys, a) - 1; per[fn[i][1] if i >= 0 else '?'] += c
tot = sum(per.values())
for n, c in per.most_common(25): print('%-28s %6.2f%%' % (n, 100 * c / tot))
print('ARM instr total', tot, 'per ms', tot / max(1, total_ms - FROM))