줄바꿈을 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

+57 -57
View File
@@ -1,57 +1,57 @@
# GBEmu
The Game Boy (DMG, black and white) on the Tamagotchi Paradise, as a further program of the Paramix menu.
No ROM here: bring your own game (.gb). Game Boy Color games are refused by the Studio.
- `src/core/peanut_gb.h`: Peanut-GB by Mahyar Koshkouei (MIT License, notice in the file), commit d0bcca7.
Changes for the Paradise: the ROM is read straight from the cached flash window (`PGB_ROM`, two places marked
"Paramix"), `__gb_read` can take an attribute (`PGB_READ_ATTR`, unused).
- `src/stub`: three small headers so Peanut-GB builds without a C library.
- `src/app/main.c`: the Paradise program (PRAM, about 34 KB). Settings block "PMXG" at 0x140: ROM, length, save
(4 KB settings of the device, then the cartridge RAM, up to 32 KB), flags (bit 0 English, bit 1 wheel the other
way round), name, keys (9 bytes, see below).
- Screen: 160 x 144 shrunk to 128 x 115, every 5 x 5 dots mixed into 4 x 4 (weights 4/1, 3/2, 2/3, 1/4 both ways),
so thin lines stay visible as a lighter shade; nothing is cut off. Black bars above and below. Every second frame
is drawn (the display takes about 30 pictures a second), only the rows that changed are sent.
- Keys: `keys[i]` are Game Boy buttons (1 A, 2 B, 4 SELECT, 8 START, 16 RIGHT, 32 LEFT, 64 UP, 128 DOWN) for the
Paradise input i: 0 A, 1 B, 2 C, 3 wheel press, 4 wheel up, 5 wheel down, 6 A+C, 7 B+C, 8 A+B. A pair that has a
meaning takes its two keys while both are held. One wheel step holds up or down for 150 ms (steps add up). The
wheel's encoder is read in the 1 ms tick, so no step is lost while a frame runs.
Default (test37): A left, B = B, C right, wheel press = A, wheel up/down, A+C START. B+C and A+B are free, so B with
a direction works (run). SELECT is a line in the settings (pressed for 200 ms when the menu closes).
Fixed: A+B+C together = settings (select, colour, sound, light, dim, screen off, restart game, language); wheel held
2 s = save and back to the Paradise.
- Save: the cartridge RAM goes to the flash 3 s after the game wrote it (1 s without a key), when the screen goes
off and on leaving. With the screen off the game waits.
- Sound (switch GB_SOUND, test37): the Paradise has one square-wave buzzer. The core keeps the sound registers; a hook
(`PGB_APU_WRITE`, marked "Paramix" in the core) sees triggers. Once per Game Boy frame the program follows length,
volume envelope and the sweep of channel 1, and plays the louder of the two square channels (channel 1 first at the
same loudness, it carries most effects). The wave channel (mostly the bass) only when both are quiet, the noise
channel never. Setting "Sound" in the device's settings. TONE_SAFE (test38, hw.c): when the new period is below the
running counter, the counter starts again (before, the buzzer stayed silent until the 32-bit counter wrapped).
GB_TONE_RESTART (test39): every new pitch stops and starts the timer (on the device a pitch changed in the running
timer stayed silent); changes below about 1.5 % (vibrato) are left out.
- Speed (test38): PGB_FAST_LINE draws background and window a tile at a time (`pgb_spread`, `pgb_pal4`, marked
"Paramix" in the core), GB_FAST shrinks four dots at once (table `g_h5`), GB_SKIP_ADAPT leaves out up to three frames
in a row when the game falls behind (normally every second frame is drawn). Same picture dot for dot.
- State (GB_STATE, 1.6.11 test2): on leaving (wheel held) the whole Game Boy goes to the flash behind the cartridge
RAM (`emu/src/emu/pstate.h`), read once at the next start and dropped right after.
- Link cable (GB_LINK, 1.6.11 test2, `src/app/gb_link.h`): the serial port between two Paradise plugged together at the
top (plink over the contacts, 460800 baud, frames 'G'). The clocking side sends its byte ('M') and keeps the transfer
open (PGB_SERIAL_WAIT, marked "Paramix" in the core) until the other side's game is ready to receive and answers with
its own byte ('R'); both clocking: 'B', both get 0xFF; no answer within 400 ms: 0xFF. Without a partner everything
is as before. Tested: `test/link2.c` (two cores on the PC, a Pokemon trade from the Cable Club to the end), and two
devgb.py joined by pipes (LINKW, LINKR) with `test/mklinkrom.py` (an own small program, 256 bytes both ways).
Speed (Unicorn simulation, 96 MHz): Super Mario Land and Kirby's Dream Land 2 at full speed (59.7 frames a second);
Blargg's cpu_instrs (no HALT at all) at about 75 %.
Build: `sh src/app/build.sh` (clang, lld) gives `gb.bin`.
Test: `python3 test/mkimg.py gb.bin game.gb img.bin` then `python3 test/devgb.py src/app/gb.elf img.bin <ms>`
(KEYS="t:mask,...", WHEEL="t:steps", SHOTS="t,...", OUT=dir); `test/prof.py` counts instructions per function.
Author: mameise
No session (1.7.3 test7, switch SESS_FLAG): flags bit 12 set by the Studio ("Remember where you left off" off): leaving writes
no state, a start drops an old one; only the game's own save (cartridge RAM or EEPROM) is kept.
# GBEmu
The Game Boy (DMG, black and white) on the Tamagotchi Paradise, as a further program of the Paramix menu.
No ROM here: bring your own game (.gb). Game Boy Color games are refused by the Studio.
- `src/core/peanut_gb.h`: Peanut-GB by Mahyar Koshkouei (MIT License, notice in the file), commit d0bcca7.
Changes for the Paradise: the ROM is read straight from the cached flash window (`PGB_ROM`, two places marked
"Paramix"), `__gb_read` can take an attribute (`PGB_READ_ATTR`, unused).
- `src/stub`: three small headers so Peanut-GB builds without a C library.
- `src/app/main.c`: the Paradise program (PRAM, about 34 KB). Settings block "PMXG" at 0x140: ROM, length, save
(4 KB settings of the device, then the cartridge RAM, up to 32 KB), flags (bit 0 English, bit 1 wheel the other
way round), name, keys (9 bytes, see below).
- Screen: 160 x 144 shrunk to 128 x 115, every 5 x 5 dots mixed into 4 x 4 (weights 4/1, 3/2, 2/3, 1/4 both ways),
so thin lines stay visible as a lighter shade; nothing is cut off. Black bars above and below. Every second frame
is drawn (the display takes about 30 pictures a second), only the rows that changed are sent.
- Keys: `keys[i]` are Game Boy buttons (1 A, 2 B, 4 SELECT, 8 START, 16 RIGHT, 32 LEFT, 64 UP, 128 DOWN) for the
Paradise input i: 0 A, 1 B, 2 C, 3 wheel press, 4 wheel up, 5 wheel down, 6 A+C, 7 B+C, 8 A+B. A pair that has a
meaning takes its two keys while both are held. One wheel step holds up or down for 150 ms (steps add up). The
wheel's encoder is read in the 1 ms tick, so no step is lost while a frame runs.
Default (test37): A left, B = B, C right, wheel press = A, wheel up/down, A+C START. B+C and A+B are free, so B with
a direction works (run). SELECT is a line in the settings (pressed for 200 ms when the menu closes).
Fixed: A+B+C together = settings (select, colour, sound, light, dim, screen off, restart game, language); wheel held
2 s = save and back to the Paradise.
- Save: the cartridge RAM goes to the flash 3 s after the game wrote it (1 s without a key), when the screen goes
off and on leaving. With the screen off the game waits.
- Sound (switch GB_SOUND, test37): the Paradise has one square-wave buzzer. The core keeps the sound registers; a hook
(`PGB_APU_WRITE`, marked "Paramix" in the core) sees triggers. Once per Game Boy frame the program follows length,
volume envelope and the sweep of channel 1, and plays the louder of the two square channels (channel 1 first at the
same loudness, it carries most effects). The wave channel (mostly the bass) only when both are quiet, the noise
channel never. Setting "Sound" in the device's settings. TONE_SAFE (test38, hw.c): when the new period is below the
running counter, the counter starts again (before, the buzzer stayed silent until the 32-bit counter wrapped).
GB_TONE_RESTART (test39): every new pitch stops and starts the timer (on the device a pitch changed in the running
timer stayed silent); changes below about 1.5 % (vibrato) are left out.
- Speed (test38): PGB_FAST_LINE draws background and window a tile at a time (`pgb_spread`, `pgb_pal4`, marked
"Paramix" in the core), GB_FAST shrinks four dots at once (table `g_h5`), GB_SKIP_ADAPT leaves out up to three frames
in a row when the game falls behind (normally every second frame is drawn). Same picture dot for dot.
- State (GB_STATE, 1.6.11 test2): on leaving (wheel held) the whole Game Boy goes to the flash behind the cartridge
RAM (`emu/src/emu/pstate.h`), read once at the next start and dropped right after.
- Link cable (GB_LINK, 1.6.11 test2, `src/app/gb_link.h`): the serial port between two Paradise plugged together at the
top (plink over the contacts, 460800 baud, frames 'G'). The clocking side sends its byte ('M') and keeps the transfer
open (PGB_SERIAL_WAIT, marked "Paramix" in the core) until the other side's game is ready to receive and answers with
its own byte ('R'); both clocking: 'B', both get 0xFF; no answer within 400 ms: 0xFF. Without a partner everything
is as before. Tested: `test/link2.c` (two cores on the PC, a Pokemon trade from the Cable Club to the end), and two
devgb.py joined by pipes (LINKW, LINKR) with `test/mklinkrom.py` (an own small program, 256 bytes both ways).
Speed (Unicorn simulation, 96 MHz): Super Mario Land and Kirby's Dream Land 2 at full speed (59.7 frames a second);
Blargg's cpu_instrs (no HALT at all) at about 75 %.
Build: `sh src/app/build.sh` (clang, lld) gives `gb.bin`.
Test: `python3 test/mkimg.py gb.bin game.gb img.bin` then `python3 test/devgb.py src/app/gb.elf img.bin <ms>`
(KEYS="t:mask,...", WHEEL="t:steps", SHOTS="t,...", OUT=dir); `test/prof.py` counts instructions per function.
Author: mameise
No session (1.7.3 test7, switch SESS_FLAG): flags bit 12 set by the Studio ("Remember where you left off" off): leaving writes
no state, a start drops an old one; only the game's own save (cartridge RAM or EEPROM) is kept.
+12 -12
View File
@@ -1,12 +1,12 @@
#!/bin/sh
# Build GBEmu for the Paradise (clang/lld, Cortex-M3). Output: ../../gb.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 -isystem ../stub -DFAST_LCD -DEXIT_KEY -DLCD_FASTCLK -DTONE_FIX -DBL_SLEEP -DPARA_BACK -DPARA_MB_KEEP -DBL_PWM -DBL_MR3RST -DGB_SOUND -DTONE_SAFE -DGB_FAST -DPGB_FAST_LINE -DGB_SKIP_ADAPT -DGB_TONE_RESTART -DGB_STATE -DGB_LINK -DPGB_SERIAL_WAIT -DGB_TRIM -DGPLAY -DSESS_FLAG $XCF"
O=""
for f in main.c $E/hw.c $E/flash.c $E/libc.c; do
n=$(basename $f .c); OPT=-Os; [ "$n" = main ] && OPT=-O2; clang $CF $OPT -c $f -o $n.o; O="$O $n.o"; done
ld.lld -T link.ld --no-rosegment $O -o gb.elf
llvm-objcopy -O binary -j .text -j .data -j .init gb.elf gb.bin
cp gb.bin ../../gb.bin; rm -f *.o gb.bin; [ -n "$KEEPELF" ] || rm -f gb.elf; ls -l ../../gb.bin
#!/bin/sh
# Build GBEmu for the Paradise (clang/lld, Cortex-M3). Output: ../../gb.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 -isystem ../stub -DFAST_LCD -DEXIT_KEY -DLCD_FASTCLK -DTONE_FIX -DBL_SLEEP -DPARA_BACK -DPARA_MB_KEEP -DBL_PWM -DBL_MR3RST -DGB_SOUND -DTONE_SAFE -DGB_FAST -DPGB_FAST_LINE -DGB_SKIP_ADAPT -DGB_TONE_RESTART -DGB_STATE -DGB_LINK -DPGB_SERIAL_WAIT -DGB_TRIM -DGPLAY -DSESS_FLAG $XCF"
O=""
for f in main.c $E/hw.c $E/flash.c $E/libc.c; do
n=$(basename $f .c); OPT=-Os; [ "$n" = main ] && OPT=-O2; clang $CF $OPT -c $f -o $n.o; O="$O $n.o"; done
ld.lld -T link.ld --no-rosegment $O -o gb.elf
llvm-objcopy -O binary -j .text -j .data -j .init gb.elf gb.bin
cp gb.bin ../../gb.bin; rm -f *.o gb.bin; [ -n "$KEEPELF" ] || rm -f gb.elf; ls -l ../../gb.bin
+12 -12
View File
@@ -1,12 +1,12 @@
#!/bin/sh
# Build GBCEmu (Game Boy Color) for the Paradise. Output: ../../gbc.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 -isystem ../stub -DFAST_LCD -DEXIT_KEY -DLCD_FASTCLK -DTONE_FIX -DBL_SLEEP -DPARA_BACK -DPARA_MB_KEEP -DBL_PWM -DBL_MR3RST -DGB_SOUND -DTONE_SAFE -DGB_SKIP_ADAPT -DGB_TONE_RESTART -DGB_STATE -DGB_LINK -DPGB_SERIAL_WAIT -DGB_TRIM -DGBC -DGB_PAGES -DGPLAY -DSESS_FLAG -DPGB_BATCH -DPGB_FAST_CGB $XCF"
O=""
for f in main.c $E/hw.c $E/flash.c $E/libc.c; do
n=$(basename $f .c); OPT=-Os; [ "$n" = main ] && OPT=-O2; clang $CF $OPT -c $f -o c_$n.o; O="$O c_$n.o"; done
ld.lld -T link.ld --no-rosegment $O -o gbc.elf
llvm-objcopy -O binary -j .text -j .data -j .init gbc.elf gbc.bin
cp gbc.bin ../../gbc.bin; rm -f c_*.o gbc.bin; [ -n "$KEEPELF" ] || rm -f gbc.elf; ls -l ../../gbc.bin
#!/bin/sh
# Build GBCEmu (Game Boy Color) for the Paradise. Output: ../../gbc.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 -isystem ../stub -DFAST_LCD -DEXIT_KEY -DLCD_FASTCLK -DTONE_FIX -DBL_SLEEP -DPARA_BACK -DPARA_MB_KEEP -DBL_PWM -DBL_MR3RST -DGB_SOUND -DTONE_SAFE -DGB_SKIP_ADAPT -DGB_TONE_RESTART -DGB_STATE -DGB_LINK -DPGB_SERIAL_WAIT -DGB_TRIM -DGBC -DGB_PAGES -DGPLAY -DSESS_FLAG -DPGB_BATCH -DPGB_FAST_CGB $XCF"
O=""
for f in main.c $E/hw.c $E/flash.c $E/libc.c; do
n=$(basename $f .c); OPT=-Os; [ "$n" = main ] && OPT=-O2; clang $CF $OPT -c $f -o c_$n.o; O="$O c_$n.o"; done
ld.lld -T link.ld --no-rosegment $O -o gbc.elf
llvm-objcopy -O binary -j .text -j .data -j .init gbc.elf gbc.bin
cp gbc.bin ../../gbc.bin; rm -f c_*.o gbc.bin; [ -n "$KEEPELF" ] || rm -f gbc.elf; ls -l ../../gbc.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},
};
+83 -83
View File
@@ -1,83 +1,83 @@
/* GBEmu link (GB_LINK, 1.6.11 test2): the Game Boy's serial port between two Paradise, as with a link cable.
* The side that clocks (SC = 0x81) sends its byte ('M') and waits; the other side answers with its own byte ('R') as
* soon as its game is ready to receive (SC = 0x80); until then it keeps the byte. Both clocking at once: each answers
* 'B' (busy) and both get 0xFF, as with two clocks on a real cable. Without a partner (no hello for 2 s) everything
* is as before: a clocking side gets 0xFF, a receiving side waits.
* The transport and the clock come from the includer:
* GL_SEND(kind, byte) send one frame to the other side
* GL_POLL() take in what arrived (calls gl_got)
* GL_NOW() milliseconds
* GL_PARTNER() 1 when the other side is there
* Header only, one link. Author: mameise */
#ifndef GB_LINK_H
#define GB_LINK_H
#define GL_WAIT_MAX 400u /* ms the clocking side waits for an answer, then 0xFF */
#define GL_PEND_MAX 600u /* ms a received byte waits for the game to get ready, then 'B' */
typedef struct GlState {
uint8_t waiting, got, reply; /* clocking side: waiting for the answer, it came, its byte */
uint8_t pend, pend_byte; /* receiving side: a byte of the other side waits */
uint32_t t0, pend_t;
uint32_t n_tx, n_rx; /* bytes (for the tests) */
} GlState;
#ifndef GL_S /* the PC test runs two: it points GL_S at the one running */
static GlState g_gl_one;
#define GL_S g_gl_one
#endif
/* a frame from the other side */
static void gl_got(uint8_t kind, uint8_t b)
{
if (kind == 'M') {
if (GL_S.waiting) { GL_SEND('B', 0xFF); return; } /* both clocking */
GL_S.pend = 1; GL_S.pend_byte = b; GL_S.pend_t = GL_NOW();
} else if (kind == 'R' || kind == 'B') {
if (GL_S.waiting) { GL_S.got = 1; GL_S.reply = kind == 'R' ? b : 0xFF; }
}
}
/* Peanut-GB: a transfer starts */
static void gl_tx(struct gb_s *gb, const uint8_t tx)
{
if (!GL_PARTNER()) return;
if (!(gb->hram_io[IO_SC] & 1u)) { /* receiving side gets ready */
GL_S.waiting = 0; /* (a clocking try of this side is over) */
GL_POLL();
if (GL_S.pend) { /* the other side clocked already: done at once */
GL_SEND('R', tx); GL_S.pend = 0; GL_S.n_rx++;
gb->hram_io[IO_SB] = GL_S.pend_byte;
gb->hram_io[IO_SC] &= 0x01;
gb->hram_io[IO_IF] |= SERIAL_INTR;
}
return;
}
if (GL_S.pend) { GL_SEND('B', 0xFF); GL_S.pend = 0; } /* the other one clocked too */
GL_S.waiting = 1; GL_S.got = 0; GL_S.t0 = GL_NOW();
GL_SEND('M', tx); GL_S.n_tx++;
}
/* Peanut-GB: the transfer's 8 bits are through (called again and again while it returns GB_SERIAL_RX_WAIT) */
static enum gb_serial_rx_ret_e gl_rx(struct gb_s *gb, uint8_t *rx)
{
GL_POLL();
if (gb->hram_io[IO_SC] & 1u) { /* clocking side */
if (!GL_S.waiting) return GB_SERIAL_RX_NO_CONNECTION; /* no partner: 0xFF */
if (GL_S.got) { GL_S.waiting = 0; *rx = GL_S.reply; GL_S.n_rx++; return GB_SERIAL_RX_SUCCESS; }
if (GL_NOW() - GL_S.t0 > GL_WAIT_MAX) { GL_S.waiting = 0; *rx = 0xFF; return GB_SERIAL_RX_SUCCESS; }
return GB_SERIAL_RX_WAIT;
}
/* receiving side: done when the other side's byte is there */
if (GL_S.pend) {
GL_SEND('R', gb->hram_io[IO_SB]); GL_S.pend = 0;
*rx = GL_S.pend_byte; GL_S.n_rx++;
return GB_SERIAL_RX_SUCCESS;
}
return GB_SERIAL_RX_NO_CONNECTION; /* keeps waiting, SB stays */
}
/* from the main loop: a byte nobody took for long is refused (the other side goes on with 0xFF) */
static void gl_tick(void)
{
GL_POLL();
if (GL_S.pend && GL_NOW() - GL_S.pend_t > GL_PEND_MAX) { GL_SEND('B', 0xFF); GL_S.pend = 0; }
}
#endif
/* GBEmu link (GB_LINK, 1.6.11 test2): the Game Boy's serial port between two Paradise, as with a link cable.
* The side that clocks (SC = 0x81) sends its byte ('M') and waits; the other side answers with its own byte ('R') as
* soon as its game is ready to receive (SC = 0x80); until then it keeps the byte. Both clocking at once: each answers
* 'B' (busy) and both get 0xFF, as with two clocks on a real cable. Without a partner (no hello for 2 s) everything
* is as before: a clocking side gets 0xFF, a receiving side waits.
* The transport and the clock come from the includer:
* GL_SEND(kind, byte) send one frame to the other side
* GL_POLL() take in what arrived (calls gl_got)
* GL_NOW() milliseconds
* GL_PARTNER() 1 when the other side is there
* Header only, one link. Author: mameise */
#ifndef GB_LINK_H
#define GB_LINK_H
#define GL_WAIT_MAX 400u /* ms the clocking side waits for an answer, then 0xFF */
#define GL_PEND_MAX 600u /* ms a received byte waits for the game to get ready, then 'B' */
typedef struct GlState {
uint8_t waiting, got, reply; /* clocking side: waiting for the answer, it came, its byte */
uint8_t pend, pend_byte; /* receiving side: a byte of the other side waits */
uint32_t t0, pend_t;
uint32_t n_tx, n_rx; /* bytes (for the tests) */
} GlState;
#ifndef GL_S /* the PC test runs two: it points GL_S at the one running */
static GlState g_gl_one;
#define GL_S g_gl_one
#endif
/* a frame from the other side */
static void gl_got(uint8_t kind, uint8_t b)
{
if (kind == 'M') {
if (GL_S.waiting) { GL_SEND('B', 0xFF); return; } /* both clocking */
GL_S.pend = 1; GL_S.pend_byte = b; GL_S.pend_t = GL_NOW();
} else if (kind == 'R' || kind == 'B') {
if (GL_S.waiting) { GL_S.got = 1; GL_S.reply = kind == 'R' ? b : 0xFF; }
}
}
/* Peanut-GB: a transfer starts */
static void gl_tx(struct gb_s *gb, const uint8_t tx)
{
if (!GL_PARTNER()) return;
if (!(gb->hram_io[IO_SC] & 1u)) { /* receiving side gets ready */
GL_S.waiting = 0; /* (a clocking try of this side is over) */
GL_POLL();
if (GL_S.pend) { /* the other side clocked already: done at once */
GL_SEND('R', tx); GL_S.pend = 0; GL_S.n_rx++;
gb->hram_io[IO_SB] = GL_S.pend_byte;
gb->hram_io[IO_SC] &= 0x01;
gb->hram_io[IO_IF] |= SERIAL_INTR;
}
return;
}
if (GL_S.pend) { GL_SEND('B', 0xFF); GL_S.pend = 0; } /* the other one clocked too */
GL_S.waiting = 1; GL_S.got = 0; GL_S.t0 = GL_NOW();
GL_SEND('M', tx); GL_S.n_tx++;
}
/* Peanut-GB: the transfer's 8 bits are through (called again and again while it returns GB_SERIAL_RX_WAIT) */
static enum gb_serial_rx_ret_e gl_rx(struct gb_s *gb, uint8_t *rx)
{
GL_POLL();
if (gb->hram_io[IO_SC] & 1u) { /* clocking side */
if (!GL_S.waiting) return GB_SERIAL_RX_NO_CONNECTION; /* no partner: 0xFF */
if (GL_S.got) { GL_S.waiting = 0; *rx = GL_S.reply; GL_S.n_rx++; return GB_SERIAL_RX_SUCCESS; }
if (GL_NOW() - GL_S.t0 > GL_WAIT_MAX) { GL_S.waiting = 0; *rx = 0xFF; return GB_SERIAL_RX_SUCCESS; }
return GB_SERIAL_RX_WAIT;
}
/* receiving side: done when the other side's byte is there */
if (GL_S.pend) {
GL_SEND('R', gb->hram_io[IO_SB]); GL_S.pend = 0;
*rx = GL_S.pend_byte; GL_S.n_rx++;
return GB_SERIAL_RX_SUCCESS;
}
return GB_SERIAL_RX_NO_CONNECTION; /* keeps waiting, SB stays */
}
/* from the main loop: a byte nobody took for long is refused (the other side goes on with 0xFF) */
static void gl_tick(void)
{
GL_POLL();
if (GL_S.pend && GL_NOW() - GL_S.pend_t > GL_PEND_MAX) { GL_SEND('B', 0xFF); GL_S.pend = 0; }
}
#endif
+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*) }
}
+737 -737
View File
File diff suppressed because it is too large. Load diff
+4167 -4167
View File
File diff suppressed because it is too large. Load diff
+4881 -4881
View File
File diff suppressed because it is too large. Load diff
+4 -4
View File
@@ -1,4 +1,4 @@
/* freestanding stubs for Peanut-GB on the Paradise */
#pragma once
#include <stddef.h>
void abort(void) __attribute__((noreturn));
/* freestanding stubs for Peanut-GB on the Paradise */
#pragma once
#include <stddef.h>
void abort(void) __attribute__((noreturn));
+4 -4
View File
@@ -1,4 +1,4 @@
#pragma once
#include <stddef.h>
void *memset(void *d, int c, size_t n);
void *memcpy(void *d, const void *s, size_t n);
#pragma once
#include <stddef.h>
void *memset(void *d, int c, size_t n);
void *memcpy(void *d, const void *s, size_t n);
+2 -2
View File
@@ -1,2 +1,2 @@
#pragma once
struct tm { int tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst; };
#pragma once
struct tm { int tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst; };
+181 -181
View File
@@ -1,181 +1,181 @@
# GBEmu (gb.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); 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]))
XIPN = [0]
def _xipw(uc, acc, a, sz, v, ud): # 1.7.2 GB_PAGES: the window follows its base register
o = v - 0x60000000
if 0 <= o < 0x1000000: uc.mem_write(0x10000000, bytes(fl[o:o + 0x100000]).ljust(0x100000, b'\xff')); XIPN[0] += 1
u.hook_add(UC_HOOK_MEM_WRITE, _xipw, begin=0x4002F004, end=0x4002F007)
import atexit; atexit.register(lambda: print('window moves', XIPN[0]))
WH = [] # WHEEL='t:steps,...' (+ one way, - the other): the encoder phases at GPIO2
for p in os.environ.get('WHEEL', '').split(','):
if ':' in p: t, n = p.split(':'); WH.append([float(t), int(n)])
state_enc = {'ph': 0, 'q': []}
GRAY = [0, 1, 3, 2]
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)
H = {'hw_px_begin': h_begin, 'hw_px_row': h_row, 'hw_px_end': h_nop, 'hw_tone': h_tone, 'hw_buttons': h_btn,
'flash_prog4k': h_prog, 'flash_erase4k': h_erase, 'hw_back_to_paradise': h_back, 'hw_init': h_nop,
'hw_backlight': h_nop, 'hw_bl_pwm_off': h_nop, 'hw_wdt_feed': h_nop}
for n, f in H.items():
a = syms[n] & ~1
u.hook_add(UC_HOOK_CODE, (lambda f: lambda uc, ad, sz, ud: f(uc))(f), begin=a, end=a)
import builtins
if hasattr(builtins, '__gbprof'): u.hook_add(UC_HOOK_BLOCK, builtins.__gbprof[0])
if os.environ.get('CACHESIM'): # test38: the I-cache of the flash window (16 KB, 32-byte lines, WAYS ways, LRU): misses per frame
_ways = int(os.environ.get('WAYS', '4')); _sets = 16384 // 32 // _ways
_cs = [[] for _ in range(_sets)]; state['miss'] = 0; state['acc'] = 0
def _cr(uc, acc, a, sz, v, ud):
state['acc'] += 1
ln = (a - 0x10000000) >> 5; s = _cs[ln % _sets]
if ln in s: s.remove(ln); s.append(ln); return
state['miss'] += 1; s.append(ln)
if len(s) > _ways: s.pop(0)
u.hook_add(UC_HOOK_MEM_READ, _cr, begin=0x10000000, end=0x100FFFFF)
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)
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))
# 1.6.11 test2 GB_LINK: two of these joined by pipes, one exchange per ms (LINKW: pipe to write, LINKR: pipe to read)
LK = {'tx': bytearray(), 'n0': 0}
if os.environ.get('LINKW'):
def _uw(uc, acc, a, sz, v, ud):
LK['n0'] += 1
if LK['n0'] > 1: LK['tx'].append(v & 0xFF) # the first is the baud divisor
u.hook_add(UC_HOOK_MEM_WRITE, _uw, begin=0x4000B000, end=0x4000B000)
LK['w'] = os.fdopen(os.open(os.environ['LINKW'], os.O_RDWR), 'wb', buffering=0); LK['r'] = os.fdopen(os.open(os.environ['LINKR'], os.O_RDWR), 'rb', buffering=0) # (O_RDWR: no wait for the other end)
def link_ms():
d = bytes(LK['tx']); LK['tx'] = bytearray()
LK['w'].write(struct.pack('<H', len(d)) + d)
h = LK['r'].read(2)
while len(h) < 2: h += LK['r'].read(2 - len(h))
n = struct.unpack('<H', h)[0]; got = b''
while len(got) < n: got += LK['r'].read(n - len(got))
if got:
a = syms['g_pl']; rt = u.mem_read(a + 257, 1)[0]
for b in got: u.mem_write(a + rt, bytes([b])); rt = (rt + 1) & 0xFF
u.mem_write(a + 257, bytes([rt]))
sp, rst = struct.unpack('<II', bytes(u.mem_read(0, 8)))
u.reg_write(UC_ARM_REG_SP, sp)
pc = rst; gms = syms['g_ms']; rtc0 = int(os.environ.get('RTC0', '5000'))
emu_ms_sym = None
si = 0
while state['ms'] < total_ms and 'back' not in state:
u.mem_write(0x45000304, struct.pack('<I', rtc0 + state['ms'] // 1000))
try:
u.emu_start(pc | 1, 0xFFFFFFF0, count=max(1, RT >> state['sh'])); state['shms'][min(state['sh'], 3)] += 1
except UcError as ex:
print('error', ex, hex(u.reg_read(UC_ARM_REG_PC))); break
pc = u.reg_read(UC_ARM_REG_PC)
state['ms'] += 1
if 'w' in LK: link_ms()
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 w in WH:
if w[1] and state['ms'] / 1000.0 >= w[0]:
d = 1 if w[1] > 0 else -1; state_enc['ph'] = (state_enc['ph'] + d) % 4; w[0] += 0.004 if (state_enc['ph'] % 4) else 0.02
if state_enc['ph'] == 0: w[1] -= d
u.mem_write(0x4001A000, struct.pack('<I', GRAY[state_enc['ph']]))
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
if 'miss' in state and state['ms'] % 1000 == 0:
_f = struct.unpack('<I', u.mem_read(syms['g_gbst'], 4))[0]; _df = max(1, _f - state.get('f0', 0))
print('s %2d frames %3d misses/frame %6.0f reads/frame %7.0f' % (state['ms'] // 1000, _df, state['miss'] / _df, state['acc'] / _df))
state['f0'] = _f; state['miss'] = 0; state['acc'] = 0
st = struct.unpack('<4I', bytes(u.mem_read(syms['g_gbst'], 16)))
print('frames run', st[0], 'drawn', st[1], 'dropped', st[2], 'last frame ms', st[3])
(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'])
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())
# GBEmu (gb.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); 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]))
XIPN = [0]
def _xipw(uc, acc, a, sz, v, ud): # 1.7.2 GB_PAGES: the window follows its base register
o = v - 0x60000000
if 0 <= o < 0x1000000: uc.mem_write(0x10000000, bytes(fl[o:o + 0x100000]).ljust(0x100000, b'\xff')); XIPN[0] += 1
u.hook_add(UC_HOOK_MEM_WRITE, _xipw, begin=0x4002F004, end=0x4002F007)
import atexit; atexit.register(lambda: print('window moves', XIPN[0]))
WH = [] # WHEEL='t:steps,...' (+ one way, - the other): the encoder phases at GPIO2
for p in os.environ.get('WHEEL', '').split(','):
if ':' in p: t, n = p.split(':'); WH.append([float(t), int(n)])
state_enc = {'ph': 0, 'q': []}
GRAY = [0, 1, 3, 2]
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)
H = {'hw_px_begin': h_begin, 'hw_px_row': h_row, 'hw_px_end': h_nop, 'hw_tone': h_tone, 'hw_buttons': h_btn,
'flash_prog4k': h_prog, 'flash_erase4k': h_erase, 'hw_back_to_paradise': h_back, 'hw_init': h_nop,
'hw_backlight': h_nop, 'hw_bl_pwm_off': h_nop, 'hw_wdt_feed': h_nop}
for n, f in H.items():
a = syms[n] & ~1
u.hook_add(UC_HOOK_CODE, (lambda f: lambda uc, ad, sz, ud: f(uc))(f), begin=a, end=a)
import builtins
if hasattr(builtins, '__gbprof'): u.hook_add(UC_HOOK_BLOCK, builtins.__gbprof[0])
if os.environ.get('CACHESIM'): # test38: the I-cache of the flash window (16 KB, 32-byte lines, WAYS ways, LRU): misses per frame
_ways = int(os.environ.get('WAYS', '4')); _sets = 16384 // 32 // _ways
_cs = [[] for _ in range(_sets)]; state['miss'] = 0; state['acc'] = 0
def _cr(uc, acc, a, sz, v, ud):
state['acc'] += 1
ln = (a - 0x10000000) >> 5; s = _cs[ln % _sets]
if ln in s: s.remove(ln); s.append(ln); return
state['miss'] += 1; s.append(ln)
if len(s) > _ways: s.pop(0)
u.hook_add(UC_HOOK_MEM_READ, _cr, begin=0x10000000, end=0x100FFFFF)
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)
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))
# 1.6.11 test2 GB_LINK: two of these joined by pipes, one exchange per ms (LINKW: pipe to write, LINKR: pipe to read)
LK = {'tx': bytearray(), 'n0': 0}
if os.environ.get('LINKW'):
def _uw(uc, acc, a, sz, v, ud):
LK['n0'] += 1
if LK['n0'] > 1: LK['tx'].append(v & 0xFF) # the first is the baud divisor
u.hook_add(UC_HOOK_MEM_WRITE, _uw, begin=0x4000B000, end=0x4000B000)
LK['w'] = os.fdopen(os.open(os.environ['LINKW'], os.O_RDWR), 'wb', buffering=0); LK['r'] = os.fdopen(os.open(os.environ['LINKR'], os.O_RDWR), 'rb', buffering=0) # (O_RDWR: no wait for the other end)
def link_ms():
d = bytes(LK['tx']); LK['tx'] = bytearray()
LK['w'].write(struct.pack('<H', len(d)) + d)
h = LK['r'].read(2)
while len(h) < 2: h += LK['r'].read(2 - len(h))
n = struct.unpack('<H', h)[0]; got = b''
while len(got) < n: got += LK['r'].read(n - len(got))
if got:
a = syms['g_pl']; rt = u.mem_read(a + 257, 1)[0]
for b in got: u.mem_write(a + rt, bytes([b])); rt = (rt + 1) & 0xFF
u.mem_write(a + 257, bytes([rt]))
sp, rst = struct.unpack('<II', bytes(u.mem_read(0, 8)))
u.reg_write(UC_ARM_REG_SP, sp)
pc = rst; gms = syms['g_ms']; rtc0 = int(os.environ.get('RTC0', '5000'))
emu_ms_sym = None
si = 0
while state['ms'] < total_ms and 'back' not in state:
u.mem_write(0x45000304, struct.pack('<I', rtc0 + state['ms'] // 1000))
try:
u.emu_start(pc | 1, 0xFFFFFFF0, count=max(1, RT >> state['sh'])); state['shms'][min(state['sh'], 3)] += 1
except UcError as ex:
print('error', ex, hex(u.reg_read(UC_ARM_REG_PC))); break
pc = u.reg_read(UC_ARM_REG_PC)
state['ms'] += 1
if 'w' in LK: link_ms()
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 w in WH:
if w[1] and state['ms'] / 1000.0 >= w[0]:
d = 1 if w[1] > 0 else -1; state_enc['ph'] = (state_enc['ph'] + d) % 4; w[0] += 0.004 if (state_enc['ph'] % 4) else 0.02
if state_enc['ph'] == 0: w[1] -= d
u.mem_write(0x4001A000, struct.pack('<I', GRAY[state_enc['ph']]))
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
if 'miss' in state and state['ms'] % 1000 == 0:
_f = struct.unpack('<I', u.mem_read(syms['g_gbst'], 4))[0]; _df = max(1, _f - state.get('f0', 0))
print('s %2d frames %3d misses/frame %6.0f reads/frame %7.0f' % (state['ms'] // 1000, _df, state['miss'] / _df, state['acc'] / _df))
state['f0'] = _f; state['miss'] = 0; state['acc'] = 0
st = struct.unpack('<4I', bytes(u.mem_read(syms['g_gbst'], 16)))
print('frames run', st[0], 'drawn', st[1], 'dropped', st[2], 'last frame ms', st[3])
(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'])
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())
+94 -94
View File
@@ -1,94 +1,94 @@
/* Two Game Boys over GBEmu's link (gb_link.h), on the PC: interleaved a scanline at a time, frames between them
* delayed by LAT scanlines. link2 <rom> <save0|-> <save1|-> <script0> <script1> <frames> <outdir>
* script: lines "frame keys" (keys: Peanut joypad bits pressed, 1 A 2 B 4 SELECT 8 START 16 R 32 L 64 U 128 D),
* "frame shot". Saves the cartridge RAM of both at the end (out/save0.sav, save1.sav). Test only. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
static uint8_t *g_rom; static size_t g_rom_n;
#define PGB_ROM g_rom
#define ENABLE_SOUND 0
#define PEANUT_GB_HIGH_LCD_ACCURACY 1
#define PGB_SERIAL_WAIT
#include "../src/core/peanut_gb.h"
static struct gb_s G[2];
static uint8_t cram[2][0x8000];
static uint8_t fb[2][144][160];
static int cur; /* which one runs */
static long lines[2]; /* scanlines run (time) */
#define LAT (getenv("LAT") ? atoi(getenv("LAT")) : 20)
typedef struct { long at; int to; uint8_t k, b; } Msg;
static Msg q[4096]; static int qn;
static long now_lines(void) { return lines[cur]; }
struct GlState; static struct GlState *gl_cur;
#define GL_S (*gl_cur)
static void gl_got(uint8_t kind, uint8_t b);
static void send_to(int to, uint8_t k, uint8_t b) { if (getenv("DBG")) fprintf(stderr, "%ld gb%d -> %c %02x\n", lines[cur], cur, k, b); if (qn < 4096) { q[qn].at = lines[cur] + LAT; q[qn].to = to; q[qn].k = k; q[qn].b = b; qn++; } }
static void poll_cur(void)
{
for (int i = 0; i < qn; i++) if (q[i].to == cur && q[i].at <= lines[cur]) { Msg m = q[i]; memmove(&q[i], &q[i + 1], (size_t)(qn - i - 1) * sizeof(Msg)); qn--; i--; gl_got(m.k, m.b); }
}
static int g_partner = 1;
#define GL_SEND(k, b) send_to(1 - cur, (k), (b))
#define GL_POLL() poll_cur()
#define GL_NOW() ((uint32_t)(lines[cur] * 1000 / 9198))
#define GL_PARTNER() g_partner
#include "../src/app/gb_link.h"
static GlState gls[2];
static uint8_t rom_rd(struct gb_s *gb, const uint_fast32_t a) { (void)gb; return a < g_rom_n ? g_rom[a] : 0xFF; }
static uint8_t cram_rd(struct gb_s *gb, const uint_fast32_t a) { return cram[gb == &G[1]][a & 0x7FFF]; }
static void cram_wr(struct gb_s *gb, const uint_fast32_t a, const uint8_t v) { cram[gb == &G[1]][a & 0x7FFF] = v; }
static void err(struct gb_s *gb, const enum gb_error_e e, const uint16_t a) { fprintf(stderr, "gb%d error %d at %04x\n", gb == &G[1], e, a); }
static void lcd(struct gb_s *gb, const uint8_t px[160], const uint_fast8_t line) { int i = gb == &G[1]; for (int x = 0; x < 160; x++) fb[i][line][x] = px[x] & 3; }
static void shot(const char *dir, int i, int f)
{
char p[512]; snprintf(p, sizeof p, "%s/g%d_%05d.pgm", dir, i, f); FILE *o = fopen(p, "wb"); fprintf(o, "P5 160 144 255\n");
static const uint8_t sh[4] = { 0xE0, 0xA8, 0x60, 0x18 };
for (int y = 0; y < 144; y++) for (int x = 0; x < 160; x++) fputc(sh[fb[i][y][x]], o); fclose(o);
}
typedef struct { int f; int keys; int shot; int pa, pv; } Ev;
static int load_script(const char *fn, Ev *ev, int max)
{
int n = 0; if (!strcmp(fn, "-")) return 0; FILE *f = fopen(fn, "r"); char l[128];
while (n < max && fgets(l, sizeof l, f)) { int fr; char w[32], a[32], v[32]; int k = sscanf(l, "%d %31s %31s %31s", &fr, w, a, v); if (k < 2) continue; ev[n].f = fr; ev[n].pa = 0;
if (!strcmp(w, "peek") && k >= 3) { ev[n].pa = (int)strtol(a, 0, 16); ev[n].pv = -1; ev[n].shot = 0; ev[n].keys = -1; n++; continue; }
if (!strcmp(w, "poke") && k == 4) { ev[n].pa = (int)strtol(a, 0, 16); ev[n].pv = (int)strtol(v, 0, 16); ev[n].shot = 0; ev[n].keys = -1; n++; continue; }
ev[n].shot = !strcmp(w, "shot"); ev[n].keys = ev[n].shot ? 0 : (int)strtol(w, 0, 0); n++; }
fclose(f); return n;
}
static uint8_t *rd(const char *f, size_t *n) { FILE *x = fopen(f, "rb"); if (!x) return 0; fseek(x, 0, 2); *n = (size_t)ftell(x); fseek(x, 0, 0); uint8_t *p = malloc(*n); if (fread(p, 1, *n, x) != *n) {} fclose(x); return p; }
int main(int argc, char **argv)
{
g_rom = rd(argv[1], &g_rom_n);
for (int i = 0; i < 2; i++) {
memset(cram[i], 0xFF, sizeof cram[i]);
if (strcmp(argv[2 + i], "-")) { size_t n; uint8_t *s = rd(argv[2 + i], &n); if (s) { memcpy(cram[i], s, n > 0x8000 ? 0x8000 : n); free(s); } }
if (gb_init(&G[i], rom_rd, cram_rd, cram_wr, err, 0) != GB_INIT_NO_ERROR) { printf("init\n"); return 1; }
gb_init_lcd(&G[i], lcd); gb_init_serial(&G[i], gl_tx, gl_rx);
G[i].direct.joypad = 0xFF;
}
static Ev ev[2][4096]; int ne[2]; ne[0] = load_script(argv[4], ev[0], 4096); ne[1] = load_script(argv[5], ev[1], 4096);
int frames = atoi(argv[6]); const char *out = argv[7];
if (getenv("SOLO")) g_partner = 0;
int fr[2] = { 0, 0 }, ei[2] = { 0, 0 };
while (fr[0] < frames || fr[1] < frames) {
for (int i = 0; i < 2; i++) {
if (fr[i] >= frames) continue;
cur = i; gl_cur = &gls[i];
uint8_t ly = G[i].hram_io[IO_LY];
G[i].gb_frame = false;
long guard = 0;
while (G[i].hram_io[IO_LY] == ly && !G[i].gb_frame && guard++ < 200000) __gb_step_cpu(&G[i]);
lines[i]++;
if (G[i].gb_frame || (G[i].hram_io[IO_LY] == 0 && ly == 153)) {
fr[i]++;
gl_tick();
while (ei[i] < ne[i] && ev[i][ei[i]].f <= fr[i]) { Ev *e = &ev[i][ei[i]++]; if (e->pa && e->pv < 0) printf("gb%d f%d peek %04x = %02x\n", i, fr[i], e->pa, __gb_read(&G[i], (uint16_t)e->pa)); else if (e->pa) __gb_write(&G[i], (uint16_t)e->pa, (uint8_t)e->pv); else if (e->shot) shot(out, i, fr[i]); else G[i].direct.joypad = (uint8_t)~e->keys; }
}
}
}
for (int i = 0; i < 2; i++) { char p[512]; snprintf(p, sizeof p, "%s/save%d.sav", out, i); FILE *o = fopen(p, "wb"); fwrite(cram[i], 1, 0x8000, o); fclose(o);
printf("gb%d frames %d tx %u rx %u\n", i, fr[i], gls[i].n_tx, gls[i].n_rx); }
return 0;
}
/* Two Game Boys over GBEmu's link (gb_link.h), on the PC: interleaved a scanline at a time, frames between them
* delayed by LAT scanlines. link2 <rom> <save0|-> <save1|-> <script0> <script1> <frames> <outdir>
* script: lines "frame keys" (keys: Peanut joypad bits pressed, 1 A 2 B 4 SELECT 8 START 16 R 32 L 64 U 128 D),
* "frame shot". Saves the cartridge RAM of both at the end (out/save0.sav, save1.sav). Test only. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
static uint8_t *g_rom; static size_t g_rom_n;
#define PGB_ROM g_rom
#define ENABLE_SOUND 0
#define PEANUT_GB_HIGH_LCD_ACCURACY 1
#define PGB_SERIAL_WAIT
#include "../src/core/peanut_gb.h"
static struct gb_s G[2];
static uint8_t cram[2][0x8000];
static uint8_t fb[2][144][160];
static int cur; /* which one runs */
static long lines[2]; /* scanlines run (time) */
#define LAT (getenv("LAT") ? atoi(getenv("LAT")) : 20)
typedef struct { long at; int to; uint8_t k, b; } Msg;
static Msg q[4096]; static int qn;
static long now_lines(void) { return lines[cur]; }
struct GlState; static struct GlState *gl_cur;
#define GL_S (*gl_cur)
static void gl_got(uint8_t kind, uint8_t b);
static void send_to(int to, uint8_t k, uint8_t b) { if (getenv("DBG")) fprintf(stderr, "%ld gb%d -> %c %02x\n", lines[cur], cur, k, b); if (qn < 4096) { q[qn].at = lines[cur] + LAT; q[qn].to = to; q[qn].k = k; q[qn].b = b; qn++; } }
static void poll_cur(void)
{
for (int i = 0; i < qn; i++) if (q[i].to == cur && q[i].at <= lines[cur]) { Msg m = q[i]; memmove(&q[i], &q[i + 1], (size_t)(qn - i - 1) * sizeof(Msg)); qn--; i--; gl_got(m.k, m.b); }
}
static int g_partner = 1;
#define GL_SEND(k, b) send_to(1 - cur, (k), (b))
#define GL_POLL() poll_cur()
#define GL_NOW() ((uint32_t)(lines[cur] * 1000 / 9198))
#define GL_PARTNER() g_partner
#include "../src/app/gb_link.h"
static GlState gls[2];
static uint8_t rom_rd(struct gb_s *gb, const uint_fast32_t a) { (void)gb; return a < g_rom_n ? g_rom[a] : 0xFF; }
static uint8_t cram_rd(struct gb_s *gb, const uint_fast32_t a) { return cram[gb == &G[1]][a & 0x7FFF]; }
static void cram_wr(struct gb_s *gb, const uint_fast32_t a, const uint8_t v) { cram[gb == &G[1]][a & 0x7FFF] = v; }
static void err(struct gb_s *gb, const enum gb_error_e e, const uint16_t a) { fprintf(stderr, "gb%d error %d at %04x\n", gb == &G[1], e, a); }
static void lcd(struct gb_s *gb, const uint8_t px[160], const uint_fast8_t line) { int i = gb == &G[1]; for (int x = 0; x < 160; x++) fb[i][line][x] = px[x] & 3; }
static void shot(const char *dir, int i, int f)
{
char p[512]; snprintf(p, sizeof p, "%s/g%d_%05d.pgm", dir, i, f); FILE *o = fopen(p, "wb"); fprintf(o, "P5 160 144 255\n");
static const uint8_t sh[4] = { 0xE0, 0xA8, 0x60, 0x18 };
for (int y = 0; y < 144; y++) for (int x = 0; x < 160; x++) fputc(sh[fb[i][y][x]], o); fclose(o);
}
typedef struct { int f; int keys; int shot; int pa, pv; } Ev;
static int load_script(const char *fn, Ev *ev, int max)
{
int n = 0; if (!strcmp(fn, "-")) return 0; FILE *f = fopen(fn, "r"); char l[128];
while (n < max && fgets(l, sizeof l, f)) { int fr; char w[32], a[32], v[32]; int k = sscanf(l, "%d %31s %31s %31s", &fr, w, a, v); if (k < 2) continue; ev[n].f = fr; ev[n].pa = 0;
if (!strcmp(w, "peek") && k >= 3) { ev[n].pa = (int)strtol(a, 0, 16); ev[n].pv = -1; ev[n].shot = 0; ev[n].keys = -1; n++; continue; }
if (!strcmp(w, "poke") && k == 4) { ev[n].pa = (int)strtol(a, 0, 16); ev[n].pv = (int)strtol(v, 0, 16); ev[n].shot = 0; ev[n].keys = -1; n++; continue; }
ev[n].shot = !strcmp(w, "shot"); ev[n].keys = ev[n].shot ? 0 : (int)strtol(w, 0, 0); n++; }
fclose(f); return n;
}
static uint8_t *rd(const char *f, size_t *n) { FILE *x = fopen(f, "rb"); if (!x) return 0; fseek(x, 0, 2); *n = (size_t)ftell(x); fseek(x, 0, 0); uint8_t *p = malloc(*n); if (fread(p, 1, *n, x) != *n) {} fclose(x); return p; }
int main(int argc, char **argv)
{
g_rom = rd(argv[1], &g_rom_n);
for (int i = 0; i < 2; i++) {
memset(cram[i], 0xFF, sizeof cram[i]);
if (strcmp(argv[2 + i], "-")) { size_t n; uint8_t *s = rd(argv[2 + i], &n); if (s) { memcpy(cram[i], s, n > 0x8000 ? 0x8000 : n); free(s); } }
if (gb_init(&G[i], rom_rd, cram_rd, cram_wr, err, 0) != GB_INIT_NO_ERROR) { printf("init\n"); return 1; }
gb_init_lcd(&G[i], lcd); gb_init_serial(&G[i], gl_tx, gl_rx);
G[i].direct.joypad = 0xFF;
}
static Ev ev[2][4096]; int ne[2]; ne[0] = load_script(argv[4], ev[0], 4096); ne[1] = load_script(argv[5], ev[1], 4096);
int frames = atoi(argv[6]); const char *out = argv[7];
if (getenv("SOLO")) g_partner = 0;
int fr[2] = { 0, 0 }, ei[2] = { 0, 0 };
while (fr[0] < frames || fr[1] < frames) {
for (int i = 0; i < 2; i++) {
if (fr[i] >= frames) continue;
cur = i; gl_cur = &gls[i];
uint8_t ly = G[i].hram_io[IO_LY];
G[i].gb_frame = false;
long guard = 0;
while (G[i].hram_io[IO_LY] == ly && !G[i].gb_frame && guard++ < 200000) __gb_step_cpu(&G[i]);
lines[i]++;
if (G[i].gb_frame || (G[i].hram_io[IO_LY] == 0 && ly == 153)) {
fr[i]++;
gl_tick();
while (ei[i] < ne[i] && ev[i][ei[i]].f <= fr[i]) { Ev *e = &ev[i][ei[i]++]; if (e->pa && e->pv < 0) printf("gb%d f%d peek %04x = %02x\n", i, fr[i], e->pa, __gb_read(&G[i], (uint16_t)e->pa)); else if (e->pa) __gb_write(&G[i], (uint16_t)e->pa, (uint8_t)e->pv); else if (e->shot) shot(out, i, fr[i]); else G[i].direct.joypad = (uint8_t)~e->keys; }
}
}
}
for (int i = 0; i < 2; i++) { char p[512]; snprintf(p, sizeof p, "%s/save%d.sav", out, i); FILE *o = fopen(p, "wb"); fwrite(cram[i], 1, 0x8000, o); fclose(o);
printf("gb%d frames %d tx %u rx %u\n", i, fr[i], gls[i].n_tx, gls[i].n_rx); }
return 0;
}
+9 -9
View File
@@ -1,9 +1,9 @@
# test image: gb.bin at 0xA0000 with its settings block, the ROM at 0x200000, the saves at 0x300000 (64 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, 0x47584D50, 1, 0x200000, len(r), 0x300000, int(sys.argv[4]) if len(sys.argv) > 4 else 0, b'GAME BOY')
fl[0xA0000 + 0x140 + 40:0xA0000 + 0x140 + 43] = bytes([1, 2, 8]) # keys A, B, C: Game Boy A, B, START (as the Studio's defaults)
open(out, 'wb').write(fl)
# test image: gb.bin at 0xA0000 with its settings block, the ROM at 0x200000, the saves at 0x300000 (64 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, 0x47584D50, 1, 0x200000, len(r), 0x300000, int(sys.argv[4]) if len(sys.argv) > 4 else 0, b'GAME BOY')
fl[0xA0000 + 0x140 + 40:0xA0000 + 0x140 + 43] = bytes([1, 2, 8]) # keys A, B, C: Game Boy A, B, START (as the Studio's defaults)
open(out, 'wb').write(fl)
+22 -22
View File
@@ -1,22 +1,22 @@
# A tiny own Game Boy program for the link test (no game data): 256 bytes over the serial port.
# Starts with the A button. role m: clocks (SC=0x81) and sends 0,1,2..; role s: answers (SC=0x80) with 0x80,0x81,..; both keep what they get at C000.
# python3 mklinkrom.py m|s out.gb
import sys
m = sys.argv[1] == 'm'
r = bytearray(32768)
r[0x100:0x104] = bytes([0x00, 0xC3, 0x50, 0x01])
r[0x134:0x13C] = b'LINKTEST'
code = [0xF3,
0x3E, 0x10, 0xE0, 0x00, 0xF0, 0x00, 0xCB, 0x47, 0x20, 0xF6, # wait for A (both sides know each other by then)
0x21, 0x00, 0xC0, 0x06, 0x00,
0x78, 0xEE, 0x00 if m else 0x80, 0xE0, 0x01, 0x3E, 0x81 if m else 0x80, 0xE0, 0x02,
0xF0, 0x02, 0xCB, 0x7F, 0x20, 0xFA,
0xF0, 0x01, 0x22, 0x04,
0x0E, 0x00, 0x0D, 0x20, 0xFD,
0x7C, 0xFE, 0xC1, 0x20, 0xE3,
0x18, 0xFE]
r[0x150:0x150 + len(code)] = bytes(code)
x = 0
for i in range(0x134, 0x14D): x = (x - r[i] - 1) & 0xFF
r[0x14D] = x
open(sys.argv[2], 'wb').write(r)
# A tiny own Game Boy program for the link test (no game data): 256 bytes over the serial port.
# Starts with the A button. role m: clocks (SC=0x81) and sends 0,1,2..; role s: answers (SC=0x80) with 0x80,0x81,..; both keep what they get at C000.
# python3 mklinkrom.py m|s out.gb
import sys
m = sys.argv[1] == 'm'
r = bytearray(32768)
r[0x100:0x104] = bytes([0x00, 0xC3, 0x50, 0x01])
r[0x134:0x13C] = b'LINKTEST'
code = [0xF3,
0x3E, 0x10, 0xE0, 0x00, 0xF0, 0x00, 0xCB, 0x47, 0x20, 0xF6, # wait for A (both sides know each other by then)
0x21, 0x00, 0xC0, 0x06, 0x00,
0x78, 0xEE, 0x00 if m else 0x80, 0xE0, 0x01, 0x3E, 0x81 if m else 0x80, 0xE0, 0x02,
0xF0, 0x02, 0xCB, 0x7F, 0x20, 0xFA,
0xF0, 0x01, 0x22, 0x04,
0x0E, 0x00, 0x0D, 0x20, 0xFD,
0x7C, 0xFE, 0xC1, 0x20, 0xE3,
0x18, 0xFE]
r[0x150:0x150 + len(code)] = bytes(code)
x = 0
for i in range(0x134, 0x14D): x = (x - r[i] - 1) & 0xFF
r[0x14D] = x
open(sys.argv[2], 'wb').write(r)
+19 -19
View File
@@ -1,19 +1,19 @@
# count instructions per function (PROF=1 in devgb): exec devgb with a block hook
import os, sys, subprocess, bisect, collections
elf = sys.argv[1]
fs = []
for l in subprocess.run(['llvm-nm', '-S', '--size-sort', elf], capture_output=True, text=True).stdout.splitlines():
p = l.split()
if len(p) == 4 and p[2] in 'tT': fs.append((int(p[0], 16) & ~1, int(p[1], 16), p[3]))
fs.sort(); starts = [f[0] for f in fs]
cnt = collections.Counter()
def blk(uc, addr, size, ud):
i = bisect.bisect_right(starts, addr) - 1
if i >= 0 and addr < fs[i][0] + fs[i][1]: cnt[fs[i][2]] += size // 2
else: cnt['?'] += size // 2
import builtins
builtins.__gbprof = (blk, cnt)
exec(open(os.path.join(os.path.dirname(__file__), 'devgb.py')).read())
tot = sum(cnt.values())
for n, c in cnt.most_common(25): print('%6.2f%% %s' % (100 * c / tot, n))
# count instructions per function (PROF=1 in devgb): exec devgb with a block hook
import os, sys, subprocess, bisect, collections
elf = sys.argv[1]
fs = []
for l in subprocess.run(['llvm-nm', '-S', '--size-sort', elf], capture_output=True, text=True).stdout.splitlines():
p = l.split()
if len(p) == 4 and p[2] in 'tT': fs.append((int(p[0], 16) & ~1, int(p[1], 16), p[3]))
fs.sort(); starts = [f[0] for f in fs]
cnt = collections.Counter()
def blk(uc, addr, size, ud):
i = bisect.bisect_right(starts, addr) - 1
if i >= 0 and addr < fs[i][0] + fs[i][1]: cnt[fs[i][2]] += size // 2
else: cnt['?'] += size // 2
import builtins
builtins.__gbprof = (blk, cnt)
exec(open(os.path.join(os.path.dirname(__file__), 'devgb.py')).read())
tot = sum(cnt.values())
for n, c in cnt.most_common(25): print('%6.2f%% %s' % (100 * c / tot, n))