다마고치 연구소 한국어판 소스 공개
This commit is contained in:
commit
db23855630
661 files changed
+503597
No files matched your search
@@ -0,0 +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.
|
||||
BIN
Binary file not shown.
@@ -0,0 +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
|
||||
@@ -0,0 +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
|
||||
@@ -0,0 +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},
|
||||
};
|
||||
@@ -0,0 +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
|
||||
@@ -0,0 +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*) }
|
||||
}
|
||||
@@ -0,0 +1,737 @@
|
||||
/* GBEmu: the Game Boy (DMG) on the Tamagotchi Paradise. Core: Peanut-GB by Mahyar Koshkouei (MIT), see ../core.
|
||||
* Started from the Paramix menu like the other further programs. Bring your own ROM (.gb).
|
||||
* Screen: 160 x 144 shrunk to 128 x 115 (5 dots to 4, mixed, nothing cut off), black bars above and below.
|
||||
* Keys: set in the Studio (tab Game Boy), stored in the settings block. A+B+C together: settings (also SELECT and
|
||||
* sound); wheel held 2 s: save and back to the Paradise. Sound (GB_SOUND): the Paradise's buzzer plays one channel.
|
||||
* Save: the cartridge RAM (battery save) goes to the flash a few seconds after the game wrote it, and on leaving.
|
||||
* Author: mameise */
|
||||
#include "../../../emu/src/emu/hw.h"
|
||||
#include "../../../emu/src/emu/exitkey.h"
|
||||
#include "../../../emu/src/emu/flash.h"
|
||||
#define PARA_ATTR
|
||||
#include "../../../emu/src/emu/paraclock.h"
|
||||
#ifdef PARA_BACK
|
||||
#include "../../../emu/src/emu/paraback.h"
|
||||
#ifdef GPLAY
|
||||
#define GP_POINTS
|
||||
#include "../../../emu/src/emu/gplay.h"
|
||||
#endif
|
||||
#endif
|
||||
typedef int s32;
|
||||
|
||||
/* ---- settings block at 0x140, filled in by the Studio ----
|
||||
* keys[i]: Game Boy buttons (Peanut-GB joypad bits: 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.
|
||||
* flags: bit 0 English, bit 1 wheel the other way round.
|
||||
* save_off: 4 KB settings of the device, then the cartridge RAM (up to 32 KB). */
|
||||
#define NIN 9
|
||||
typedef struct { u32 magic, version, rom_off, rom_len, save_off, flags; char name[16]; u8 keys[12]; } GbCfg;
|
||||
#define GB_MAGIC 0x47584D50u /* "PMXG" */
|
||||
__attribute__((section(".pmxcfg"), used))
|
||||
volatile const GbCfg g_cfg = { GB_MAGIC, 1, 0, 0, 0, 0, {0}, { 0x20, 0x02, 0x10, 0x01, 0x40, 0x80, 0x08, 0x00, 0x00 } };
|
||||
#define SETTINGS (g_cfg.save_off)
|
||||
#define CRAM_AT (g_cfg.save_off + 0x1000u)
|
||||
#define CRAM_MAX 0x8000u
|
||||
#define XIP_BASE_REG REG(0x4002F004u)
|
||||
#define XIP_CTRL_REG REG(0x4002F000u)
|
||||
#define ROMWIN ((const u8 *)0x10000000u)
|
||||
|
||||
#define PGB_ROM ROMWIN
|
||||
#define ENABLE_SOUND 0
|
||||
#define PEANUT_GB_HIGH_LCD_ACCURACY 0
|
||||
#ifdef GB_SOUND
|
||||
static void apu_write(u32 addr, u8 val);
|
||||
#define PGB_APU_WRITE(a, v) apu_write((u32)(a), (u8)(v))
|
||||
#endif
|
||||
#ifdef GB_TRIM
|
||||
/* 1.7.1: the offset of the switchable ROM bank is worked out when the game switches the bank (before: on every read).
|
||||
* Banks behind the stored part of the game read the last stored one (the Studio keeps equal end banks once). */
|
||||
struct gb_s;
|
||||
static u32 g_romoff, g_blast = 0xFFFFu;
|
||||
#ifdef GB_PAGES
|
||||
static u32 g_page;
|
||||
#endif
|
||||
static void bank_set(struct gb_s *gb);
|
||||
#define PGB_ROMOFF g_romoff
|
||||
#define PGB_BANKSET(gb) bank_set(gb)
|
||||
#endif
|
||||
#ifdef GBC
|
||||
/* 1.7.2: Game Boy Color (the core's cgb branch, PEANUT_FULL_GBC_SUPPORT): colours, two VRAM banks, eight WRAM banks,
|
||||
* double speed. A DMG game runs in this program as well (as on the real Game Boy Color). */
|
||||
#define PEANUT_FULL_GBC_SUPPORT 1
|
||||
static int g_pal_dirty = 1;
|
||||
#define PGB_PALW(gb) (g_pal_dirty = 1)
|
||||
#include "../core/peanut_gbc.h"
|
||||
#else
|
||||
#include "../core/peanut_gb.h"
|
||||
#endif
|
||||
#include "font.h"
|
||||
#ifdef GB_TRIM
|
||||
__attribute__((noinline)) static void bank_set(struct gb_s *gb)
|
||||
{
|
||||
u32 b = (gb->mbc == 1 && gb->cart_mode_select) ? (gb->selected_rom_bank & 0x1Fu) : gb->selected_rom_bank;
|
||||
#ifdef GB_PAGES
|
||||
/* 1.7.2: games over 1 MB (the cached window shows 1 MB): stored as pages of 1 MB, each bank 0 and 63 switchable
|
||||
* banks (flags bit 2); the window moves when the game switches to a bank of another page */
|
||||
if (g_cfg.flags & 4u) {
|
||||
if (!b) b = 1u;
|
||||
u32 pg = (b - 1u) / 63u;
|
||||
if (pg != g_page) { g_page = pg; XIP_BASE_REG = 0x60000000u | (g_cfg.rom_off + (pg << 20)); XIP_CTRL_REG = 3u; __asm__ volatile("dsb\n isb" ::: "memory"); }
|
||||
g_romoff = ((b - 1u) % 63u) * ROM_BANK_SIZE;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (b > g_blast) b = g_blast;
|
||||
g_romoff = (b - 1u) * ROM_BANK_SIZE;
|
||||
}
|
||||
#define ROM_LEN_FULL ((g_cfg.flags >> 16) ? (g_cfg.flags >> 16) * ROM_BANK_SIZE : g_cfg.rom_len)
|
||||
#else
|
||||
#define ROM_LEN_FULL g_cfg.rom_len
|
||||
#endif
|
||||
|
||||
/* ---- time: SysTick 1 ms; the wheel's encoder is read here too, so no step is lost while a frame runs ---- */
|
||||
static volatile u32 g_ms;
|
||||
static u32 g_tone_now; /* buzzer frequency now (GB_SOUND) */
|
||||
static volatile s32 g_enc_acc; static u32 g_enc_last;
|
||||
static const signed char QD[16] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 };
|
||||
#define GPIO2 0x4001A000u
|
||||
#define SYST_CSR REG(0xE000E010u)
|
||||
#define SYST_RVR REG(0xE000E014u)
|
||||
#define SYST_CVR REG(0xE000E018u)
|
||||
#define CORE_HZ 96000000u
|
||||
__attribute__((used)) void systick_c(void)
|
||||
{
|
||||
g_ms++;
|
||||
u32 n = REG(GPIO2) & 3u;
|
||||
if (n != g_enc_last) { g_enc_acc += QD[(g_enc_last << 2) | n]; g_enc_last = n; }
|
||||
}
|
||||
__attribute__((naked)) void SysTick_Handler(void) { __asm__ volatile("b systick_c\n"); }
|
||||
void abort(void) { for (;;) hw_wdt_feed(); }
|
||||
|
||||
/* ---- settings on the device ---- */
|
||||
typedef struct { u8 magic[4]; u8 pal, dim, lang, dimto, off, r0, skip, mute; } GbSet; /* mute: 0 = sound on */
|
||||
static GbSet g_set = { {'G','B','S','1'}, 0, 7, 0, 6, 3, 0x80, 0, 0 };
|
||||
/* light to dark: DMG green, pocket grey, light (yellow), blue */
|
||||
static const u16 PAL[4][4] = {
|
||||
{ 0x9DE1u, 0x8D61u, 0x3324u, 0x0981u },
|
||||
{ 0xE71Cu, 0xAD55u, 0x5ACBu, 0x10A2u },
|
||||
{ 0xFFF4u, 0xCE4Cu, 0x7B46u, 0x2100u },
|
||||
{ 0xDF5Fu, 0x8D3Cu, 0x3A92u, 0x0846u } };
|
||||
static const char *const PAL_NM[2][4] = { { "GRUEN", "GRAU", "GELB", "BLAU" }, { "GREEN", "GREY", "YELLOW", "BLUE" } };
|
||||
#define T_N 7
|
||||
static const u32 T_MS[T_N] = { 10000u, 20000u, 30000u, 60000u, 120000u, 300000u, 600000u };
|
||||
static const char *const T_NM[2][T_N + 1] = { { "10 S", "20 S", "30 S", "1 MIN", "2 MIN", "5 MIN", "10 MIN", "NIE" }, { "10 S", "20 S", "30 S", "1 MIN", "2 MIN", "5 MIN", "10 MIN", "NEVER" } };
|
||||
#define LV_N 11
|
||||
#define DIM2_N 10
|
||||
static const u8 LV_PCT[LV_N] = { 100u, 5u, 10u, 20u, 30u, 40u, 50u, 60u, 70u, 80u, 90u };
|
||||
static const u8 DIM2_PCT[DIM2_N] = { 1u, 2u, 3u, 5u, 10u, 15u, 20u, 30u, 40u, 50u };
|
||||
static int en(void) { return g_set.lang ? g_set.lang == 2u : (int)(g_cfg.flags & 1u); }
|
||||
static u16 BG, INK, OFF;
|
||||
#ifdef GBC
|
||||
static void gbc_colours(const u16 *p);
|
||||
#endif
|
||||
static u16 g_lut[76]; /* mixed brightness 0..75 (25 weights x shade 0..3) to colour */
|
||||
static int g_dimmed;
|
||||
static u16 mix(u16 a, u16 b, int p, int q) /* a + (b - a) * p / q */
|
||||
{
|
||||
int r = (a >> 11) + (((b >> 11) - (a >> 11)) * p) / q, g = ((a >> 5) & 63) + ((((b >> 5) & 63) - ((a >> 5) & 63)) * p) / q;
|
||||
int bl = (a & 31) + (((b & 31) - (a & 31)) * p) / q;
|
||||
return (u16)((r << 11) | (g << 5) | bl);
|
||||
}
|
||||
static void colours(void)
|
||||
{
|
||||
const u16 *p = PAL[g_set.pal & 3u];
|
||||
for (int v = 0; v < 76; v++) { int s = v / 25, f = v % 25; g_lut[v] = s >= 3 ? p[3] : mix(p[s], p[s + 1], f, 25); }
|
||||
BG = p[0]; INK = p[3]; OFF = p[2];
|
||||
#ifdef GBC
|
||||
gbc_colours(p);
|
||||
#endif
|
||||
#ifdef BL_PWM
|
||||
{ u32 l = LV_PCT[(g_set.r0 & 15u) % LV_N], d = DIM2_PCT[g_set.dimto % DIM2_N];
|
||||
hw_bl_set(g_dimmed ? (d < l ? d : l) : l); }
|
||||
#endif
|
||||
}
|
||||
static u8 g_sec[0x1000] __attribute__((aligned(4)));
|
||||
static void set_load(void)
|
||||
{
|
||||
const volatile u8 *p = XIP_PTR(SETTINGS); GbSet t; u8 *d = (u8 *)&t;
|
||||
for (u32 i = 0; i < sizeof t; i++) d[i] = p[i];
|
||||
if (t.magic[0] == 'G' && t.magic[1] == 'B' && t.magic[2] == 'S' && t.magic[3] == '1' && t.pal < 4u && t.dim <= T_N && t.off <= T_N && t.lang < 3u) g_set = t;
|
||||
colours();
|
||||
}
|
||||
static void set_store(void)
|
||||
{
|
||||
for (u32 i = 0; i < sizeof g_sec; i++) g_sec[i] = 0xFF;
|
||||
const u8 *s = (const u8 *)&g_set; for (u32 i = 0; i < sizeof g_set; i++) g_sec[i] = s[i];
|
||||
flash_update4k(SETTINGS, g_sec);
|
||||
}
|
||||
|
||||
/* ---- the Game Boy ---- */
|
||||
static struct gb_s g_gb;
|
||||
static u8 g_cram[CRAM_MAX] __attribute__((aligned(4)));
|
||||
static u32 g_cram_len; static int g_cram_dirty;
|
||||
static u8 gb_rom_rd(struct gb_s *gb, const uint_fast32_t a) { (void)gb; return ROMWIN[a]; }
|
||||
static u8 gb_cram_rd(struct gb_s *gb, const uint_fast32_t a) { (void)gb; return a < CRAM_MAX ? g_cram[a] : 0xFF; }
|
||||
static void gb_cram_wr(struct gb_s *gb, const uint_fast32_t a, const u8 v) { (void)gb; if (a < CRAM_MAX && g_cram[a] != v) { g_cram[a] = v; g_cram_dirty = 1; } }
|
||||
static volatile u32 g_err;
|
||||
static void gb_err(struct gb_s *gb, const enum gb_error_e e, const uint16_t a) { (void)gb; g_err = 0x80000000u | (u32)e << 16 | a; }
|
||||
static void cram_load(void)
|
||||
{
|
||||
const volatile u8 *p = XIP_PTR(CRAM_AT);
|
||||
for (u32 i = 0; i < g_cram_len; i++) g_cram[i] = p[i];
|
||||
/* a fresh save area (all 0xFF) is what a new cartridge has too */
|
||||
}
|
||||
static void cram_store(void)
|
||||
{
|
||||
for (u32 o = 0; o < g_cram_len; o += 0x1000u) {
|
||||
u32 n = g_cram_len - o < 0x1000u ? g_cram_len - o : 0x1000u;
|
||||
for (u32 i = 0; i < 0x1000u; i++) g_sec[i] = i < n ? g_cram[o + i] : 0xFF;
|
||||
hw_tone(0); g_tone_now = 0;
|
||||
flash_update4k(CRAM_AT + o, g_sec);
|
||||
}
|
||||
g_cram_dirty = 0;
|
||||
}
|
||||
|
||||
#ifdef GB_SOUND
|
||||
/* ---- sound (GB_SOUND, test37): the Paradise has one square-wave buzzer, so of the Game Boy's four channels the
|
||||
* loudest of the two square channels plays (the wave channel when they are quiet; the noise channel never).
|
||||
* The registers are kept by the core (hram_io); triggers, length, volume envelope and the sweep of channel 1 are
|
||||
* followed here once per Game Boy frame (16.7 ms). ---- */
|
||||
typedef struct { u8 on, vol, env_dir, env_per, env_t; u16 len_ms; u8 len_on; u16 freq; u8 sw_t; } ApuCh;
|
||||
static ApuCh g_ch[3];
|
||||
static u8 *io(u32 a) { return &g_gb.hram_io[a - 0xFF00u]; }
|
||||
static void apu_write(u32 addr, u8 val)
|
||||
{
|
||||
int c = addr >= 0xFF10u && addr <= 0xFF14u ? 0 : addr >= 0xFF15u && addr <= 0xFF19u ? 1 : addr >= 0xFF1Au && addr <= 0xFF1Eu ? 2 : -1;
|
||||
if (addr == 0xFF26u && !(val & 0x80u)) { g_ch[0].on = g_ch[1].on = g_ch[2].on = 0; return; }
|
||||
if (c < 0) return;
|
||||
u32 base = c == 0 ? 0xFF10u : c == 1 ? 0xFF15u : 0xFF1Au;
|
||||
ApuCh *h = &g_ch[c];
|
||||
if (addr == base + 4u) {
|
||||
h->len_on = (val & 0x40u) != 0;
|
||||
if (val & 0x80u) { /* trigger */
|
||||
if (c < 2) { u8 e = *io(base + 2u); h->vol = e >> 4; h->env_dir = (e & 8u) != 0; h->env_per = e & 7u; h->env_t = 0; h->on = (e & 0xF8u) != 0;
|
||||
h->len_ms = (u16)((64u - (*io(base + 1u) & 63u)) * 1000u / 256u); }
|
||||
else { h->on = (*io(0xFF1Au) & 0x80u) != 0; h->len_ms = (u16)((256u - *io(0xFF1Bu)) * 1000u / 256u); }
|
||||
h->freq = (u16)(*io(base + 3u) | (val & 7u) << 8); h->sw_t = 0;
|
||||
}
|
||||
}
|
||||
if (c < 2 && addr == base + 2u && !(val & 0xF8u)) h->on = 0; /* DAC off */
|
||||
if (c == 2 && addr == 0xFF1Au && !(val & 0x80u)) h->on = 0;
|
||||
}
|
||||
/* one Game Boy frame (16.74 ms) on: length, envelope (steps of 1/64 s), sweep (steps of 1/128 s); then the buzzer */
|
||||
static void apu_frame(void)
|
||||
{
|
||||
for (int c = 0; c < 3; c++) {
|
||||
ApuCh *h = &g_ch[c]; if (!h->on) continue;
|
||||
u32 base = c == 0 ? 0xFF10u : c == 1 ? 0xFF15u : 0xFF1Au;
|
||||
if (!h->sw_t || c) h->freq = (u16)(*io(base + 3u) | (*io(base + 4u) & 7u) << 8); /* vibrato: the game changes the registers */
|
||||
if (h->len_on) { if (h->len_ms <= 17u) { h->on = 0; continue; } h->len_ms = (u16)(h->len_ms - 17u); }
|
||||
if (c < 2 && h->env_per) { h->env_t++; if (h->env_t >= h->env_per) { h->env_t = 0; if (h->env_dir) { if (h->vol < 15u) h->vol++; } else if (h->vol) h->vol--; } }
|
||||
if (c == 0) { /* sweep: two 128 Hz steps per frame */
|
||||
u8 sw = *io(0xFF10u); u32 per = (sw >> 4) & 7u, sh = sw & 7u;
|
||||
if (per && sh) for (int k = 0; k < 2; k++) { if (++h->sw_t >= per) { h->sw_t = 1; u32 d = h->freq >> sh, f = (sw & 8u) ? h->freq - d : h->freq + d; if (f > 2047u) { h->on = 0; break; } h->freq = (u16)f; } }
|
||||
}
|
||||
}
|
||||
u32 hz = 0;
|
||||
if (!g_set.mute && (*io(0xFF26u) & 0x80u)) {
|
||||
u8 pan = *io(0xFF25u); int best = -1; u32 bv = 0;
|
||||
for (int c = 0; c < 3; c++) {
|
||||
ApuCh *h = &g_ch[c]; if (!h->on || !(pan & (0x11u << c))) continue;
|
||||
u32 v = c < 2 ? h->vol : ((*io(0xFF1Cu) >> 5) & 3u) == 1u ? 15u : ((*io(0xFF1Cu) >> 5) & 3u) == 2u ? 7u : ((*io(0xFF1Cu) >> 5) & 3u) == 3u ? 3u : 0u;
|
||||
if (c == 2 && best >= 0) continue; /* the wave channel (mostly the bass) only when both square channels are quiet */
|
||||
if (v > bv) { bv = v; best = c; }
|
||||
}
|
||||
if (best >= 0) { u32 x = g_ch[best].freq & 2047u; hz = best < 2 ? 131072u / (2048u - x) : 65536u / (2048u - x); if (hz < 40u || hz > 10000u) hz = 0; }
|
||||
}
|
||||
#ifdef GB_TONE_RESTART
|
||||
/* test39: on the device a new pitch written into the running timer left the buzzer silent (the first notes with
|
||||
* silence between them played, later notes did not). Now every new pitch starts the timer anew; changes below
|
||||
* about 1.5 % (vibrato) are left out so it does not restart every frame. */
|
||||
if (hz && g_tone_now) { u32 d = hz > g_tone_now ? hz - g_tone_now : g_tone_now - hz; if (d * 64u < g_tone_now) return; }
|
||||
if (hz != g_tone_now) { if (hz && g_tone_now) hw_tone(0); g_tone_now = hz; hw_tone(hz); }
|
||||
#else
|
||||
if (hz != g_tone_now) { g_tone_now = hz; hw_tone(hz); }
|
||||
#endif
|
||||
}
|
||||
static void apu_quiet(void) { if (g_tone_now) { g_tone_now = 0; hw_tone(0); } }
|
||||
#else
|
||||
static void apu_quiet(void) {}
|
||||
#endif
|
||||
|
||||
/* ---- screen: 5 x 5 Game Boy dots to 4 x 4 (weights 4/1, 3/2, 2/3, 1/4), 128 x 115 from y 6 ---- */
|
||||
#define OY 6
|
||||
#define OH 115
|
||||
static int g_frame_drawn, g_dirty_lo = OH, g_dirty_hi = -1;
|
||||
#ifdef GBC
|
||||
/* 1.7.2 GBC: colour dots (RGB 5:6:5 in the picture), mixed per channel with the same weights */
|
||||
static u16 g_fbc[OH][128] __attribute__((aligned(4)));
|
||||
/* one colour packed in a word: red bits 0..9, green 10..20, blue 21..30 (room for the sums of 25 weights) */
|
||||
static u32 g_hc[128]; /* the current line, shrunk across (sums of 5 weights) */
|
||||
static u32 g_ac[2][128]; /* rows being summed (25 weights) */
|
||||
static u32 g_pk[64]; /* the palette packed */
|
||||
static u16 g_d25r[776], g_d25g[1576]; static u8 g_d25b[776]; /* sum of 25 weights to the 5:6:5 field */
|
||||
static void d25_init(void) { for (u32 i = 0; i < 776u; i++) { g_d25r[i] = (u16)((i / 25u) << 11); g_d25b[i] = (u8)(i / 25u); } for (u32 i = 0; i < 1576u; i++) g_d25g[i] = (u16)((i / 25u) << 5); }
|
||||
static u16 g_dmgc[4]; /* a DMG game: its four shades in the chosen colours */
|
||||
static void crow_out(int r, const u32 *a)
|
||||
{
|
||||
if (r >= OH) return;
|
||||
u16 *f = g_fbc[r]; u32 d = 0;
|
||||
for (int x = 0; x < 128; x++) { /* / 25 through tables */
|
||||
u32 c = a[x];
|
||||
u16 v = (u16)(g_d25r[c & 1023u] | g_d25g[(c >> 10) & 2047u] | g_d25b[c >> 21]);
|
||||
d |= f[x] ^ v; f[x] = v;
|
||||
}
|
||||
if (d) { if (r < g_dirty_lo) g_dirty_lo = r; if (r > g_dirty_hi) g_dirty_hi = r; }
|
||||
}
|
||||
static void lcd_line(struct gb_s *gb, const uint8_t px[160], const uint_fast8_t line)
|
||||
{
|
||||
static int cgb0 = -1;
|
||||
if (g_pal_dirty || cgb0 != gb->cgb.cgbMode) { g_pal_dirty = 0; cgb0 = gb->cgb.cgbMode;
|
||||
if (gb->cgb.cgbMode) for (int i = 0; i < 64; i++) { u32 v = gb->cgb.fixPalette[i]; g_pk[i] = ((v >> 10) & 31u) | ((v >> 5) & 31u) << 11 | (v & 31u) << 21; }
|
||||
else for (int i = 0; i < 64; i++) { u32 v = g_dmgc[i & 3]; g_pk[i] = (v >> 11) | ((v >> 5) & 63u) << 10 | (v & 31u) << 21; } }
|
||||
const uint8_t *s = px; u32 *h = g_hc;
|
||||
for (int k = 0; k < 32; k++, s += 5, h += 4) {
|
||||
u32 a = g_pk[s[0] & 63u], b = g_pk[s[1] & 63u], c = g_pk[s[2] & 63u], d = g_pk[s[3] & 63u], e = g_pk[s[4] & 63u];
|
||||
h[0] = 4u * a + b; h[1] = 3u * b + 2u * c; h[2] = 2u * c + 3u * d; h[3] = d + 4u * e;
|
||||
}
|
||||
int g = line / 5, l = line % 5, r = g * 4;
|
||||
u32 *a0 = g_ac[0], *a1 = g_ac[1]; const u32 *hc = g_hc;
|
||||
switch (l) {
|
||||
case 0: for (int x = 0; x < 128; x++) a0[x] = hc[x] << 2; break;
|
||||
case 1: for (int x = 0; x < 128; x++) { a0[x] += hc[x]; a1[x] = 3u * hc[x]; } crow_out(r, a0); break;
|
||||
case 2: for (int x = 0; x < 128; x++) { a1[x] += 2u * hc[x]; a0[x] = 2u * hc[x]; } crow_out(r + 1, a1); break;
|
||||
case 3: for (int x = 0; x < 128; x++) { a0[x] += 3u * hc[x]; a1[x] = hc[x]; } crow_out(r + 2, a0); break;
|
||||
default: for (int x = 0; x < 128; x++) a1[x] += hc[x] << 2; crow_out(r + 3, a1); break;
|
||||
}
|
||||
if (line == 143) g_frame_drawn = 1;
|
||||
}
|
||||
static void gbc_colours(const u16 *p)
|
||||
{
|
||||
for (int k = 0; k < 4; k++) g_dmgc[k] = p[k];
|
||||
g_pal_dirty = 1;
|
||||
g_dirty_lo = 0; g_dirty_hi = OH - 1;
|
||||
}
|
||||
#else
|
||||
static u8 g_fb[OH][128] __attribute__((aligned(4)));
|
||||
#endif
|
||||
#if defined(GB_FAST) && !defined(GBC)
|
||||
/* test38: the same weights four dots at a time (each byte of a word is one dot, sums stay below 256) */
|
||||
static u32 g_hlw[32], g_accw[2][32];
|
||||
static u32 g_h5[1024]; /* five dots (2 bits each) to their four shrunk dots */
|
||||
static void h5_init(void)
|
||||
{
|
||||
for (u32 i = 0; i < 1024u; i++) {
|
||||
u32 a = i & 3u, b = (i >> 2) & 3u, c = (i >> 4) & 3u, d = (i >> 6) & 3u, e = i >> 8;
|
||||
g_h5[i] = (4u * a + b) | (3u * b + 2u * c) << 8 | (2u * c + 3u * d) << 16 | (d + 4u * e) << 24;
|
||||
}
|
||||
}
|
||||
static void row_out(int r, const u32 *a)
|
||||
{
|
||||
if (r >= OH) return;
|
||||
u32 *f = (u32 *)g_fb[r], d = 0;
|
||||
for (int x = 0; x < 32; x++) { d |= f[x] ^ a[x]; f[x] = a[x]; }
|
||||
if (d) { if (r < g_dirty_lo) g_dirty_lo = r; if (r > g_dirty_hi) g_dirty_hi = r; }
|
||||
}
|
||||
static void lcd_line(struct gb_s *gb, const uint8_t px[160], const uint_fast8_t line)
|
||||
{
|
||||
(void)gb;
|
||||
const uint8_t *s = px;
|
||||
for (int k = 0; k < 32; k++, s += 5)
|
||||
g_hlw[k] = g_h5[(s[0] & 3u) | (s[1] & 3u) << 2 | (s[2] & 3u) << 4 | (s[3] & 3u) << 6 | (s[4] & 3u) << 8];
|
||||
int g = line / 5, l = line % 5, r = g * 4;
|
||||
u32 *a0 = g_accw[0], *a1 = g_accw[1]; const u32 *h = g_hlw;
|
||||
switch (l) {
|
||||
case 0: for (int x = 0; x < 32; x++) a0[x] = h[x] << 2; break;
|
||||
case 1: for (int x = 0; x < 32; x++) { a0[x] += h[x]; a1[x] = 3u * h[x]; } row_out(r, a0); break;
|
||||
case 2: for (int x = 0; x < 32; x++) { a1[x] += 2u * h[x]; a0[x] = 2u * h[x]; } row_out(r + 1, a1); break;
|
||||
case 3: for (int x = 0; x < 32; x++) { a0[x] += 3u * h[x]; a1[x] = h[x]; } row_out(r + 2, a0); break;
|
||||
default: for (int x = 0; x < 32; x++) a1[x] += h[x] << 2; row_out(r + 3, a1); break;
|
||||
}
|
||||
if (line == 143) g_frame_drawn = 1;
|
||||
}
|
||||
#elif !defined(GBC)
|
||||
static u8 g_hl[128]; /* the current line, shrunk across (0..15) */
|
||||
static u8 g_acc[2][128]; /* rows being summed */
|
||||
static void lcd_line(struct gb_s *gb, const uint8_t px[160], const uint_fast8_t line)
|
||||
{
|
||||
(void)gb;
|
||||
const uint8_t *s = px; u8 *h = g_hl;
|
||||
for (int k = 0; k < 32; k++, s += 5, h += 4) {
|
||||
u32 a = s[0] & 3u, b = s[1] & 3u, c = s[2] & 3u, d = s[3] & 3u, e = s[4] & 3u;
|
||||
h[0] = (u8)(4u * a + b); h[1] = (u8)(3u * b + 2u * c); h[2] = (u8)(2u * c + 3u * d); h[3] = (u8)(d + 4u * e);
|
||||
}
|
||||
int g = line / 5, l = line % 5, r = g * 4; /* out rows r..r+3 */
|
||||
u8 *a0 = g_acc[0], *a1 = g_acc[1];
|
||||
switch (l) {
|
||||
case 0: for (int x = 0; x < 128; x++) a0[x] = (u8)(4u * g_hl[x]); break;
|
||||
case 1: for (int x = 0; x < 128; x++) { a0[x] = (u8)(a0[x] + g_hl[x]); a1[x] = (u8)(3u * g_hl[x]); } if (r < OH) { for (int x = 0; x < 128; x++) if (g_fb[r][x] != a0[x]) { g_fb[r][x] = a0[x]; if (r < g_dirty_lo) g_dirty_lo = r; if (r > g_dirty_hi) g_dirty_hi = r; } } break;
|
||||
case 2: for (int x = 0; x < 128; x++) { a1[x] = (u8)(a1[x] + 2u * g_hl[x]); a0[x] = (u8)(2u * g_hl[x]); } r += 1; if (r < OH) { for (int x = 0; x < 128; x++) if (g_fb[r][x] != a1[x]) { g_fb[r][x] = a1[x]; if (r < g_dirty_lo) g_dirty_lo = r; if (r > g_dirty_hi) g_dirty_hi = r; } } break;
|
||||
case 3: for (int x = 0; x < 128; x++) { a0[x] = (u8)(a0[x] + 3u * g_hl[x]); a1[x] = g_hl[x]; } r += 2; if (r < OH) { for (int x = 0; x < 128; x++) if (g_fb[r][x] != a0[x]) { g_fb[r][x] = a0[x]; if (r < g_dirty_lo) g_dirty_lo = r; if (r > g_dirty_hi) g_dirty_hi = r; } } break;
|
||||
default: for (int x = 0; x < 128; x++) a1[x] = (u8)(a1[x] + 4u * g_hl[x]); r += 3; if (r < OH) { for (int x = 0; x < 128; x++) if (g_fb[r][x] != a1[x]) { g_fb[r][x] = a1[x]; if (r < g_dirty_lo) g_dirty_lo = r; if (r > g_dirty_hi) g_dirty_hi = r; } } break;
|
||||
}
|
||||
if (line == 143) g_frame_drawn = 1;
|
||||
}
|
||||
#endif
|
||||
static u16 g_row[128] __attribute__((aligned(4)));
|
||||
static void push(int all)
|
||||
{
|
||||
int lo = all ? 0 : g_dirty_lo, hi = all ? OH - 1 : g_dirty_hi;
|
||||
if (all) { /* the bars */
|
||||
for (int x = 0; x < 128; x++) g_row[x] = 0;
|
||||
hw_px_begin(0, 0, 128, OY); for (int y = 0; y < OY; y++) hw_px_row(g_row, 128); hw_px_end();
|
||||
hw_px_begin(0, OY + OH, 128, 128); for (int y = OY + OH; y < 128; y++) hw_px_row(g_row, 128); hw_px_end();
|
||||
}
|
||||
if (lo > hi) return;
|
||||
hw_px_begin(0, (u32)(OY + lo), 128, (u32)(OY + hi + 1));
|
||||
#ifdef GBC
|
||||
for (int y = lo; y <= hi; y++) hw_px_row(g_fbc[y], 128);
|
||||
#else
|
||||
for (int y = lo; y <= hi; y++) { const u8 *f = g_fb[y]; for (int x = 0; x < 128; x++) g_row[x] = g_lut[f[x]]; hw_px_row(g_row, 128); }
|
||||
#endif
|
||||
hw_px_end();
|
||||
g_dirty_lo = OH; g_dirty_hi = -1;
|
||||
}
|
||||
|
||||
/* ---- the settings menu (A+B+C): A next line, B change, C close ---- */
|
||||
static int g_menu, g_msel, g_mtop;
|
||||
static void text(int x, int y, const char *s, u16 col, u16 *row, int ry)
|
||||
{
|
||||
for (; *s; s++, x += 6) {
|
||||
int c = (unsigned char)*s; if (c >= 97 && c <= 122) c -= 32; c = (c < 32 || c > 127) ? 0 : c - 32;
|
||||
if (ry < y || ry >= y + 7) continue;
|
||||
u8 bits = g_font[c][ry - y];
|
||||
for (int k = 0; k < 5; k++) if ((bits & (0x10 >> k)) && x + k >= 0 && x + k < 128) row[x + k] = col;
|
||||
}
|
||||
}
|
||||
enum { M_SEL, M_PAL,
|
||||
#ifdef GB_SOUND
|
||||
M_SND,
|
||||
#endif
|
||||
M_LIGHT, M_DIM, M_DIMTO, M_OFF, M_RESET, M_LANG, M_N };
|
||||
static u32 g_select_until; /* menu line SELECT: pressed for 200 ms after the menu closes */
|
||||
static int g_items[M_N], g_nitems;
|
||||
static void menu_items(void)
|
||||
{
|
||||
g_nitems = 0;
|
||||
for (int k = 0; k < M_N; k++) { if (k == M_DIMTO && g_set.dim >= T_N) continue; g_items[g_nitems++] = k; }
|
||||
if (g_msel >= g_nitems) g_msel = g_nitems - 1;
|
||||
}
|
||||
static void pct(char *o, int v) { int n = 0; if (v >= 100) o[n++] = '1'; if (v >= 10) o[n++] = (char)('0' + (v / 10) % 10); o[n++] = (char)('0' + v % 10); o[n++] = '%'; o[n] = 0; }
|
||||
static int g_reset_ask;
|
||||
static void menu_line(int it, const char **a, char *b)
|
||||
{
|
||||
int e = en(); const char *v = 0; b[0] = 0;
|
||||
switch (it) {
|
||||
case M_SEL: *a = e ? "PRESS SELECT" : "SELECT DRUECKEN"; v = "B"; break;
|
||||
case M_PAL: *a = e ? "COLOUR" : "FARBE"; v = PAL_NM[e][g_set.pal & 3u]; break;
|
||||
#ifdef GB_SOUND
|
||||
case M_SND: *a = e ? "SOUND" : "TON"; v = g_set.mute ? (e ? "OFF" : "AUS") : (e ? "ON" : "AN"); break;
|
||||
#endif
|
||||
case M_LIGHT: *a = e ? "LIGHT" : "LICHT"; pct(b, LV_PCT[(g_set.r0 & 15u) % LV_N]); break;
|
||||
case M_DIM: *a = e ? "DIM AFTER" : "DIMMEN NACH"; v = T_NM[e][g_set.dim <= T_N ? g_set.dim : T_N]; break;
|
||||
case M_DIMTO: *a = e ? "DIM TO" : "DIMMEN AUF"; pct(b, DIM2_PCT[g_set.dimto % DIM2_N]); break;
|
||||
case M_OFF: *a = e ? "SCREEN OFF" : "BILD AUS NACH"; v = T_NM[e][g_set.off <= T_N ? g_set.off : T_N]; break;
|
||||
case M_RESET: *a = e ? "RESTART GAME" : "SPIEL NEU"; v = g_reset_ask ? (e ? "SURE? B" : "SICHER? B") : ""; break;
|
||||
default: *a = e ? "LANGUAGE" : "SPRACHE"; v = e ? "ENGLISH" : "DEUTSCH"; break;
|
||||
}
|
||||
if (v) { int n = 0; while (v[n] && n < 11) { b[n] = v[n]; n++; } b[n] = 0; }
|
||||
}
|
||||
#define ROWS 9
|
||||
static void menu_draw(void)
|
||||
{
|
||||
if (g_msel < g_mtop) g_mtop = g_msel;
|
||||
if (g_msel >= g_mtop + ROWS) g_mtop = g_msel - ROWS + 1;
|
||||
hw_px_begin(0, 0, 128, 128);
|
||||
for (int y = 0; y < 128; y++) {
|
||||
for (int x = 0; x < 128; x++) g_row[x] = BG;
|
||||
text(4, 3, en() ? "SETTINGS" : "EINSTELLUNGEN", INK, g_row, y);
|
||||
if (y == 12) for (int x = 0; x < 128; x++) g_row[x] = OFF;
|
||||
for (int k = 0; k < ROWS && g_mtop + k < g_nitems; k++) {
|
||||
int i = g_mtop + k, ly = 16 + k * 11; const char *a; char b[12]; menu_line(g_items[i], &a, b);
|
||||
if (i == g_msel && y >= ly - 2 && y < ly + 9) for (int x = 0; x < 128; x++) g_row[x] = INK;
|
||||
u16 c = i == g_msel ? BG : INK;
|
||||
text(4, ly, a, c, g_row, y); int w = 0; while (b[w]) w++; text(124 - w * 6, ly, b, c, g_row, y);
|
||||
}
|
||||
text(4, 118, en() ? "A NEXT B SET C OK" : "A ZEILE B SETZEN C OK", OFF, g_row, y);
|
||||
hw_px_row(g_row, 128);
|
||||
}
|
||||
hw_px_end();
|
||||
}
|
||||
static int g_set_dirty;
|
||||
static void menu_close(void) { g_menu = 0; g_reset_ask = 0; if (g_set_dirty) { set_store(); g_set_dirty = 0; } push(1); }
|
||||
static void menu_key(u32 k)
|
||||
{
|
||||
if (k & BTN_A) { g_msel = (g_msel + 1) % g_nitems; g_reset_ask = 0; }
|
||||
if (k & BTN_B) {
|
||||
switch (g_items[g_msel]) {
|
||||
case M_SEL: g_select_until = g_ms + 200u; menu_close(); return;
|
||||
case M_PAL: g_set.pal = (u8)((g_set.pal + 1u) & 3u); break;
|
||||
#ifdef GB_SOUND
|
||||
case M_SND: g_set.mute ^= 1u; break;
|
||||
#endif
|
||||
case M_LIGHT: g_set.r0 = (u8)(0x80u | (((g_set.r0 & 15u) + 1u) % LV_N)); break;
|
||||
case M_DIM: g_set.dim = (u8)((g_set.dim + 1u) % (T_N + 1u)); if (g_set.dim < T_N && g_set.off < T_N && g_set.off <= g_set.dim) g_set.off = (u8)(g_set.dim + 1u); break;
|
||||
case M_DIMTO: g_set.dimto = (u8)((g_set.dimto + 1u) % DIM2_N); break;
|
||||
case M_OFF: do g_set.off = (u8)((g_set.off + 1u) % (T_N + 1u)); while (g_set.off < T_N && g_set.dim < T_N && g_set.off <= g_set.dim); break;
|
||||
case M_RESET: if (g_reset_ask) { g_reset_ask = 0; gb_reset(&g_gb); menu_close(); return; } g_reset_ask = 1; break;
|
||||
default: g_set.lang = (u8)(en() ? 1u : 2u); break;
|
||||
}
|
||||
if (g_items[g_msel] != M_RESET && g_items[g_msel] != M_SEL) g_set_dirty = 1;
|
||||
colours(); menu_items();
|
||||
}
|
||||
if (k & BTN_C) { menu_close(); return; }
|
||||
menu_draw();
|
||||
}
|
||||
static void message(const char *a, const char *b)
|
||||
{
|
||||
hw_px_begin(0, 0, 128, 128);
|
||||
for (int y = 0; y < 128; y++) { for (int x = 0; x < 128; x++) g_row[x] = BG; text(4, 50, a, INK, g_row, y); text(4, 62, b, INK, g_row, y); hw_px_row(g_row, 128); }
|
||||
hw_px_end();
|
||||
}
|
||||
|
||||
/* ---- keys: the Paradise inputs to the Game Boy's buttons (map from the Studio) ---- */
|
||||
static u32 g_wheel_until[2]; /* wheel up / down held until (ms) */
|
||||
static u32 gb_keys(u32 b, u32 now)
|
||||
{
|
||||
u32 in = 0;
|
||||
int ac = (b & (BTN_A | BTN_C)) == (BTN_A | BTN_C), bc = (b & (BTN_B | BTN_C)) == (BTN_B | BTN_C), ab = (b & (BTN_A | BTN_B)) == (BTN_A | BTN_B);
|
||||
/* a pair held: its two keys count as the pair only (if the pair has a meaning) */
|
||||
u32 used = 0;
|
||||
if (ac && g_cfg.keys[6]) { in |= 1u << 6; used |= BTN_A | BTN_C; }
|
||||
if (bc && g_cfg.keys[7]) { in |= 1u << 7; used |= BTN_B | BTN_C; }
|
||||
if (ab && g_cfg.keys[8]) { in |= 1u << 8; used |= BTN_A | BTN_B; }
|
||||
u32 r = b & ~used;
|
||||
if (r & BTN_A) in |= 1u; if (r & BTN_B) in |= 2u; if (r & BTN_C) in |= 4u; if (b & BTN_WHEEL) in |= 8u;
|
||||
if ((s32)(g_wheel_until[0] - now) > 0) in |= 16u;
|
||||
if ((s32)(g_wheel_until[1] - now) > 0) in |= 32u;
|
||||
u32 gb = 0;
|
||||
for (int i = 0; i < NIN; i++) if (in & (1u << i)) gb |= g_cfg.keys[i];
|
||||
if ((s32)(g_select_until - now) > 0) gb |= 4u;
|
||||
return gb;
|
||||
}
|
||||
|
||||
#ifdef GB_LINK
|
||||
/* ---- 1.6.11 test2: link cable between two Paradise (plink over the contacts), e.g. trading in Pokemon ---- */
|
||||
#include "../../../emu/src/emu/plink.h"
|
||||
static void gl_send2(u8 k, u8 b) { u8 d[2] = { k, b }; plink_send('G', d, 2); }
|
||||
static void gl_poll2(void);
|
||||
#define GL_SEND(k, b) gl_send2((k), (b))
|
||||
#define GL_POLL() gl_poll2()
|
||||
#define GL_NOW() g_ms
|
||||
#define GL_PARTNER() (g_pl.last_rx && g_ms - g_pl.last_rx < 2500u)
|
||||
#include "gb_link.h"
|
||||
static void gl_got2(u8 ty, const u8 *d, int n) { if (ty == 'G' && n == 2) gl_got(d[0], d[1]); }
|
||||
static void gl_poll2(void) { plink_poll(g_ms, 0, gl_got2); }
|
||||
#endif
|
||||
|
||||
#ifdef GB_STATE
|
||||
/* ---- 1.6.11 test2: the whole Game Boy on leaving (wheel held), read once at the next start ---- */
|
||||
#include "../../../emu/src/emu/pstate.h"
|
||||
#ifdef SESS_FLAG
|
||||
#define NOSESS ((g_cfg.flags & 0x1000u) != 0u) /* 1.7.3 test7: the Studio's "remember on leaving" off: only the game's own save */
|
||||
#else
|
||||
#define NOSESS 0
|
||||
#endif
|
||||
#define STATE_AT (g_cfg.save_off + 0x9000u)
|
||||
#ifdef GBC
|
||||
#define STATE_MAX 0xF000u /* 1.7.2: save area 96 KB (settings, cartridge RAM, state) */
|
||||
#else
|
||||
#define STATE_MAX 0x7000u
|
||||
#endif
|
||||
static int g_running;
|
||||
static u32 st_tag(void) { return 0x47420001u ^ (u32)&g_gb ^ ((u32)sizeof(struct gb_s) << 8) ^ ROM_LEN_FULL ^ (u32)lcd_line; } /* GB_TRIM: the whole game's length, so a stored state stays valid */
|
||||
static int st_parts(StPart *pt)
|
||||
{
|
||||
int n = 0; pt[n].p = &g_gb; pt[n++].n = sizeof g_gb;
|
||||
#ifdef GB_SOUND
|
||||
pt[n].p = g_ch; pt[n++].n = sizeof g_ch;
|
||||
#endif
|
||||
return n;
|
||||
}
|
||||
static void state_store(void) { StPart pt[2]; int n = st_parts(pt); if (g_running) st_store(STATE_AT, STATE_MAX, st_tag(), pt, n, g_sec); }
|
||||
static int state_load(void)
|
||||
{
|
||||
if (NOSESS) { st_drop(STATE_AT); return 0; }
|
||||
StPart pt[2]; int n = st_parts(pt);
|
||||
struct gb_s keep = g_gb; /* the pointers of this program stay */
|
||||
int ok = st_load(STATE_AT, STATE_MAX, st_tag(), pt, n);
|
||||
if (ok) {
|
||||
g_gb.gb_rom_read = keep.gb_rom_read; g_gb.gb_cart_ram_read = keep.gb_cart_ram_read; g_gb.gb_cart_ram_write = keep.gb_cart_ram_write;
|
||||
g_gb.gb_error = keep.gb_error; g_gb.gb_serial_tx = keep.gb_serial_tx; g_gb.gb_serial_rx = keep.gb_serial_rx;
|
||||
g_gb.gb_bootrom_read = keep.gb_bootrom_read; g_gb.display.lcd_draw_line = keep.display.lcd_draw_line; g_gb.direct.priv = keep.direct.priv;
|
||||
}
|
||||
PGB_BANKSET(&g_gb);
|
||||
st_drop(STATE_AT);
|
||||
return ok;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ---- leaving ---- */
|
||||
static void leave(void)
|
||||
{
|
||||
apu_quiet(); hw_tone(0);
|
||||
if (g_set_dirty) set_store();
|
||||
REG(0x20000FF8u) = 0;
|
||||
REG(0x20000FFCu) = 0x4C584D50u; /* "PMXL": back on purpose, the autostart stays off */
|
||||
#ifdef GB_STATE
|
||||
if (g_running && !NOSESS) { message(en() ? "SAVING" : "SPEICHERN", ""); state_store(); }
|
||||
#endif
|
||||
if (g_cram_dirty) cram_store();
|
||||
#ifdef PARA_BACK
|
||||
#ifdef GPLAY
|
||||
para_points((uint8_t *)g_sec); /* 1.7.2 test14: play time to Tamapoints */
|
||||
#endif
|
||||
para_back_fix(g_sec);
|
||||
#endif
|
||||
hw_bl_pwm_off();
|
||||
for (u32 t = 0; hw_buttons() && t < 20000000u; t++) if ((t & 0xFFFFu) == 0) hw_wdt_feed();
|
||||
XIP_BASE_REG = 0x60011000u; XIP_CTRL_REG = 3u;
|
||||
hw_back_to_paradise();
|
||||
}
|
||||
|
||||
__attribute__((used)) volatile u32 g_gbst[4]; /* frames run, frames drawn, frames dropped, last frame in ms (for the tests) */
|
||||
extern u32 __bss_start, __bss_end;
|
||||
void Reset_Handler(void)
|
||||
{
|
||||
__asm__ volatile("cpsid i");
|
||||
u32 cnt = REG(0x45000304u), para = para_clock(cnt, 0, 0x1F000u, 0, 0);
|
||||
if (!para) para = para_mailbox(cnt);
|
||||
#ifdef PARA_FLASH
|
||||
if (!para) para = para_flash(cnt); /* 1.6.9 test37 */
|
||||
#endif
|
||||
#ifdef PARA_BACK
|
||||
para_leave_mark(cnt, para);
|
||||
#endif
|
||||
__asm__ volatile("" ::: "memory");
|
||||
for (u32 *p = &__bss_start; p < &__bss_end; p++) *p = 0;
|
||||
{ extern u32 __data_start, __data_end, __data_load;
|
||||
const u32 *src = &__data_load; for (u32 *p = &__data_start; p < &__data_end; p++) *p = *src++; }
|
||||
(void)para;
|
||||
hw_init();
|
||||
hw_tone(0);
|
||||
XIP_BASE_REG = 0x60000000u | g_cfg.rom_off; /* the ROM in the cached window from 0x10000000 */
|
||||
XIP_CTRL_REG = 3u;
|
||||
REG(GPIO2 + 0x04) &= ~3u; g_enc_last = REG(GPIO2) & 3u;
|
||||
set_load();
|
||||
#ifdef GB_TRIM
|
||||
if ((g_cfg.flags >> 16) && g_cfg.rom_len >= ROM_BANK_SIZE) g_blast = g_cfg.rom_len / ROM_BANK_SIZE - 1u;
|
||||
#endif
|
||||
enum gb_init_error_e ie = gb_init(&g_gb, gb_rom_rd, gb_cram_rd, gb_cram_wr, gb_err, 0);
|
||||
if (ie != GB_INIT_NO_ERROR) {
|
||||
message(en() ? "ROM NOT USABLE" : "ROM GEHT NICHT", ie == GB_INIT_CARTRIDGE_UNSUPPORTED ? (en() ? "CARTRIDGE TYPE" : "MODUL-TYP") : (en() ? "CHECKSUM" : "PRUEFSUMME"));
|
||||
for (u32 t = 0; t < 30000000u; t++) if ((t & 0xFFFFu) == 0) hw_wdt_feed();
|
||||
leave();
|
||||
}
|
||||
#ifdef GBC
|
||||
{ size_t n = gb_get_save_size(&g_gb);
|
||||
#else
|
||||
{ size_t n = 0; if (gb_get_save_size_s(&g_gb, &n) != 0) n = 0;
|
||||
#endif
|
||||
g_cram_len = n > CRAM_MAX ? CRAM_MAX : (u32)n; }
|
||||
cram_load();
|
||||
#ifdef GB_FAST
|
||||
h5_init();
|
||||
#endif
|
||||
#ifdef GBC
|
||||
d25_init();
|
||||
#endif
|
||||
gb_init_lcd(&g_gb, lcd_line);
|
||||
#ifdef GB_LINK
|
||||
plink_init(); gb_init_serial(&g_gb, gl_tx, gl_rx);
|
||||
#endif
|
||||
#ifdef GB_STATE
|
||||
state_load(); g_running = 1;
|
||||
#endif
|
||||
push(1);
|
||||
SYST_RVR = CORE_HZ / 1000u - 1u; SYST_CVR = 0; SYST_CSR = 7;
|
||||
__asm__ volatile("cpsie i");
|
||||
|
||||
u32 last_in = g_ms, wheel_ms = 0, cram_ms = 0, next_ms = g_ms, last_push = g_ms, mk0 = 0;
|
||||
u32 frac = 0;
|
||||
#ifdef GB_SKIP_ADAPT
|
||||
int skip_run = 0;
|
||||
#endif
|
||||
int bl = 1, lock = 1, menu_lock = 0;
|
||||
for (;;) {
|
||||
hw_wdt_feed();
|
||||
u32 now = g_ms;
|
||||
u32 b = hw_buttons();
|
||||
if (lock) { if (b) b = 0; else lock = 0; }
|
||||
/* wheel steps: one step = 4 phases; each step holds up or down for 150 ms */
|
||||
{ s32 a = g_enc_acc;
|
||||
if (a >= 4 || a <= -4) {
|
||||
__asm__ volatile("cpsid i"); g_enc_acc -= a >= 4 ? 4 : -4; __asm__ volatile("cpsie i");
|
||||
int up = (a > 0) ^ ((g_cfg.flags & 2u) != 0);
|
||||
u32 *u = &g_wheel_until[up ? 0 : 1]; *u = ((s32)(*u - now) > 0 ? *u : now) + 150u;
|
||||
last_in = now;
|
||||
if (g_menu) { g_msel = up ? (g_msel + g_nitems - 1) % g_nitems : (g_msel + 1) % g_nitems; menu_draw(); }
|
||||
} }
|
||||
if (b) {
|
||||
last_in = now;
|
||||
if (!bl || g_dimmed) { /* the key only wakes */
|
||||
bl = 1; hw_backlight(1); if (g_dimmed) { g_dimmed = 0; colours(); }
|
||||
if (g_menu) menu_draw(); else push(1);
|
||||
while (hw_buttons()) hw_wdt_feed();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/* wheel held 2 s: save and back */
|
||||
if (b & BTN_WHEEL) { if (!wheel_ms) wheel_ms = now ? now : 1; else if (EXIT_NOW(now - wheel_ms, 2000u)) leave(); } else wheel_ms = 0;
|
||||
/* A+B+C: settings */
|
||||
if ((b & (BTN_A | BTN_B | BTN_C)) == (BTN_A | BTN_B | BTN_C) && !g_menu && !menu_lock) {
|
||||
apu_quiet(); g_menu = 1; menu_lock = 1; g_msel = 1; g_mtop = 0; menu_items(); menu_draw(); mk0 = BTN_A | BTN_B | BTN_C; g_gb.direct.joypad = 0xFF;
|
||||
}
|
||||
if (!(b & (BTN_A | BTN_B | BTN_C))) menu_lock = 0;
|
||||
if (g_menu) {
|
||||
u32 k = b & (BTN_A | BTN_B | BTN_C), nk = k & ~mk0; mk0 = k; if (nk) menu_key(nk);
|
||||
next_ms = now;
|
||||
__asm__ volatile("wfi");
|
||||
continue;
|
||||
}
|
||||
if (!bl) { apu_quiet(); next_ms = now; __asm__ volatile("wfi"); continue; } /* screen off: the game waits (battery) */
|
||||
g_gb.direct.joypad = (u8)~gb_keys(b, now);
|
||||
/* one Game Boy frame every 16.74 ms (70224 cycles at 4.194304 MHz); drawn only when there is time */
|
||||
if ((s32)(now - next_ms) >= 0) {
|
||||
if ((s32)(now - next_ms) > 100) { g_gbst[2] += (now - next_ms) / 17u; next_ms = now; } /* too far behind: drop the time */
|
||||
int late = (s32)(now - next_ms) > 8;
|
||||
(void)late; g_gb.direct.frame_skip = 1; /* every second frame is drawn: the display takes 30 pictures a second anyway */
|
||||
g_frame_drawn = 0;
|
||||
u32 t0 = g_ms;
|
||||
gb_run_frame(&g_gb);
|
||||
g_gbst[0]++; g_gbst[3] = g_ms - t0;
|
||||
#ifdef GB_SOUND
|
||||
apu_frame();
|
||||
#endif
|
||||
frac += 740u; next_ms += 16u + frac / 1000u; frac %= 1000u; /* 16.74 ms */
|
||||
#ifdef GB_SKIP_ADAPT
|
||||
/* test38: normally every second frame is drawn; when the game falls behind (many sprites), up to three in a row
|
||||
* are left out, so the game keeps its speed and only the picture gets fewer updates */
|
||||
{ int behind = (s32)(g_ms - next_ms) > 0, draw = skip_run >= 3 || (skip_run >= 1 && !behind);
|
||||
skip_run = draw ? 0 : skip_run + 1; g_gb.display.frame_skip_count = draw; }
|
||||
#endif
|
||||
if (g_frame_drawn) g_gbst[1]++;
|
||||
if (g_err) { message(en() ? "GAME BOY ERROR" : "GAME BOY FEHLER", ""); g_err = 0; }
|
||||
if (bl && g_frame_drawn && now - last_push >= 30u) { last_push = now; push(0); }
|
||||
}
|
||||
#ifdef GB_LINK
|
||||
gl_tick();
|
||||
#endif
|
||||
if (g_cram_dirty && !cram_ms) cram_ms = now ? now : 1;
|
||||
if (cram_ms && now - cram_ms > 3000u && now - last_in > 1000u) { cram_ms = 0; cram_store(); }
|
||||
if (bl && !g_dimmed && g_set.dim < T_N && now - last_in > T_MS[g_set.dim]) { g_dimmed = 1; colours(); push(1); }
|
||||
if (bl && g_set.off < T_N && now - last_in > T_MS[g_set.off]) { bl = 0; hw_backlight(0); if (g_cram_dirty) { cram_ms = 0; cram_store(); } }
|
||||
if ((s32)(next_ms - g_ms) > 0) __asm__ volatile("wfi");
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((naked)) static void Default_Handler(void) { __asm__ volatile("1: b 1b\n"); }
|
||||
__attribute__((naked)) static void Irq_Off(void) { __asm__ volatile("cpsid i\n bx lr\n"); }
|
||||
__attribute__((section(".vectors"), used))
|
||||
const void *const g_vectors[16 + 64] = { (void *)0x18020000u, (void *)Reset_Handler,
|
||||
[2 ... 10] = (void *)Default_Handler, [11] = (void *)Irq_Off, [12 ... 13] = (void *)Default_Handler, [14] = (void *)Irq_Off,
|
||||
[15] = (void *)SysTick_Handler, [16 ... 79] = (void *)Irq_Off
|
||||
#ifdef GB_LINK
|
||||
, [16 + 37] = (void *)plink_irq
|
||||
#endif
|
||||
};
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,4 @@
|
||||
/* freestanding stubs for Peanut-GB on the Paradise */
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
void abort(void) __attribute__((noreturn));
|
||||
@@ -0,0 +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);
|
||||
@@ -0,0 +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; };
|
||||
@@ -0,0 +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())
|
||||
@@ -0,0 +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;
|
||||
}
|
||||
@@ -0,0 +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)
|
||||
@@ -0,0 +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)
|
||||
@@ -0,0 +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))
|
||||
Reference in new issue
Block a user