From cda7e95a2800df220b8ec2c863365c7a65a125fa Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Sat, 3 Jul 2021 22:18:34 +0100 Subject: [PATCH 1/9] patch in cycles output --- mgba-test-runner/add_cycles_register.patch | 40 ++++++++++++++++++++++ mgba-test-runner/build-mgba.sh | 2 ++ mgba-test-runner/build.rs | 5 +++ 3 files changed, 47 insertions(+) create mode 100644 mgba-test-runner/add_cycles_register.patch diff --git a/mgba-test-runner/add_cycles_register.patch b/mgba-test-runner/add_cycles_register.patch new file mode 100644 index 0000000..5e767fe --- /dev/null +++ b/mgba-test-runner/add_cycles_register.patch @@ -0,0 +1,40 @@ +diff --git a/include/mgba/internal/gba/io.h b/include/mgba/internal/gba/io.h +index 9875061f3..bdeafdcd3 100644 +--- a/include/mgba/internal/gba/io.h ++++ b/include/mgba/internal/gba/io.h +@@ -157,6 +157,7 @@ enum GBAIORegisters { + REG_DEBUG_STRING = 0xFFF600, + REG_DEBUG_FLAGS = 0xFFF700, + REG_DEBUG_ENABLE = 0xFFF780, ++ REG_DEBUG_CYCLES = 0xFFF800, + }; + + mLOG_DECLARE_CATEGORY(GBA_IO); +diff --git a/src/gba/io.c b/src/gba/io.c +index cc39e1192..d34dcb4b4 100644 +--- a/src/gba/io.c ++++ b/src/gba/io.c +@@ -573,6 +573,11 @@ void GBAIOWrite(struct GBA* gba, uint32_t address, uint16_t value) { + case REG_DEBUG_ENABLE: + gba->debug = value == 0xC0DE; + return; ++ case REG_DEBUG_CYCLES: { ++ int32_t number_of_cycles = mTimingCurrentTime(&gba->timing); ++ mLOG(GBA_DEBUG, INFO, "Cycles: %d Tag: %hd", number_of_cycles, value); ++ return; ++ } + case REG_DEBUG_FLAGS: + if (gba->debug) { + GBADebug(gba, value); +@@ -936,6 +941,11 @@ uint16_t GBAIORead(struct GBA* gba, uint32_t address) { + return 0x1DEA; + } + // Fall through ++ case REG_DEBUG_CYCLES: { ++ int32_t number_of_cycles = mTimingCurrentTime(&gba->timing); ++ mLOG(GBA_DEBUG, INFO, "Cycles: %d", number_of_cycles); ++ return number_of_cycles; ++ } + default: + mLOG(GBA_IO, GAME_ERROR, "Read from unused I/O register: %03X", address); + return GBALoadBad(gba->cpu); diff --git a/mgba-test-runner/build-mgba.sh b/mgba-test-runner/build-mgba.sh index 298a856..da488c5 100644 --- a/mgba-test-runner/build-mgba.sh +++ b/mgba-test-runner/build-mgba.sh @@ -2,12 +2,14 @@ MGBA_VERSION=$1 OUT_DIRECTORY=$2 +CURRENT_DIRECTORY=$(pwd) cd ${OUT_DIRECTORY} curl -L https://github.com/mgba-emu/mgba/archive/refs/tags/${MGBA_VERSION}.tar.gz -o mgba-${MGBA_VERSION}.tar.gz tar -xvf mgba-${MGBA_VERSION}.tar.gz cd mgba-${MGBA_VERSION} rm -rf build +patch --strip=1 < ${CURRENT_DIRECTORY}/add_cycles_register.patch mkdir -p build cd build cmake .. \ diff --git a/mgba-test-runner/build.rs b/mgba-test-runner/build.rs index 6d37341..086e8b9 100644 --- a/mgba-test-runner/build.rs +++ b/mgba-test-runner/build.rs @@ -29,4 +29,9 @@ fn main() { bindings .write_to_file(&out_path.join("runner-bindings.rs")) .expect("Couldn't write bindings!"); + + println!("cargo:rerun-if-changed=c/test-runner.c"); + println!("cargo:rerun-if-changed=c/test-runner.h"); + println!("cargo:rerun-if-changed=build-mgba.sh"); + println!("cargo:rerun-if-changed=add_cycles_register.patch"); } From 744c7a8bb48e807379705a8738180490519026af Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Sat, 3 Jul 2021 22:18:53 +0100 Subject: [PATCH 2/9] get cycles --- agb/src/lib.rs | 4 ++++ agb/src/mgba.rs | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/agb/src/lib.rs b/agb/src/lib.rs index edf1de2..286b007 100644 --- a/agb/src/lib.rs +++ b/agb/src/lib.rs @@ -89,7 +89,9 @@ where mgba::DebugLevel::Info, ) .unwrap(); + mgba::number_of_cycles_tagged(785); self(gba); + mgba::number_of_cycles_tagged(785); mgba.print(format_args!("[ok]"), mgba::DebugLevel::Info) .unwrap(); } @@ -138,6 +140,8 @@ pub extern "C" fn main() -> ! { #[cfg(test)] fn assert_image_output(image: &str) { + display::busy_wait_for_VBlank(); + display::busy_wait_for_VBlank(); let mut mgba = crate::mgba::Mgba::new().unwrap(); mgba.print( format_args!("image:{}", image), diff --git a/agb/src/mgba.rs b/agb/src/mgba.rs index ae88122..380efdf 100644 --- a/agb/src/mgba.rs +++ b/agb/src/mgba.rs @@ -26,6 +26,12 @@ fn is_running_in_mgba() -> bool { DEBUG_ENABLE.get() == ENABLE_HANDSHAKE_OUT } +const NUMBER_OF_CYCLES: MemoryMapped = unsafe { MemoryMapped::new(0x04FF_F800) }; + +pub fn number_of_cycles_tagged(tag: u16) { + NUMBER_OF_CYCLES.set(tag) +} + pub struct Mgba { bytes_written: usize, } From 220ddac2806181cd92cf3877bdb4dd77936cb04a Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Sat, 3 Jul 2021 22:19:02 +0100 Subject: [PATCH 3/9] use different optimisation settings --- agb/Cargo.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/agb/Cargo.toml b/agb/Cargo.toml index 2c7847a..d1f2eb9 100644 --- a/agb/Cargo.toml +++ b/agb/Cargo.toml @@ -7,11 +7,10 @@ description = "Library for Game Boy Advance Development" license = "MPL-2.0" [profile.dev] -opt-level = 0 +opt-level = 3 debug = true [profile.release] -panic = "abort" lto = true From 08eb39a64b1f478e019ac6d8f873a010b2a55e0b Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Sat, 3 Jul 2021 22:19:10 +0100 Subject: [PATCH 4/9] add output of cycles --- mgba-test-runner/src/main.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/mgba-test-runner/src/main.rs b/mgba-test-runner/src/main.rs index d91d629..97ead86 100644 --- a/mgba-test-runner/src/main.rs +++ b/mgba-test-runner/src/main.rs @@ -16,12 +16,22 @@ enum Status { Sucess, } +enum Timing { + None, + WaitFor(i32), + Difference(i32), +} + +const TEST_RUNNER_TAG: u16 = 785; + fn test_file(file_to_run: &str) -> Status { let mut finished = Status::Running; let debug_reader_mutex = Regex::new(r"(?s)^\[(.*)\] GBA Debug: (.*)$").unwrap(); + let tagged_cycles_reader = Regex::new(r"Cycles: (\d*) Tag: (\d*)").unwrap(); let mut mgba = runner::MGBA::new(file_to_run).unwrap(); let video_buffer = mgba.get_video_buffer(); + let mut number_of_cycles = Timing::None; mgba.set_logger(|message| { if let Some(captures) = debug_reader_mutex.captures(message) { @@ -41,6 +51,29 @@ fn test_file(file_to_run: &str) -> Status { } else if out.ends_with("...") { print!("{}", out); io::stdout().flush().expect("can't flush stdout"); + } else if out.starts_with("Cycles: ") { + if let Some(captures) = tagged_cycles_reader.captures(out) { + let num_cycles: i32 = captures[1].parse().unwrap(); + let tag: u16 = captures[2].parse().unwrap(); + + if tag == TEST_RUNNER_TAG { + number_of_cycles = match number_of_cycles { + Timing::WaitFor(n) => Timing::Difference(num_cycles - n), + Timing::None => Timing::WaitFor(num_cycles), + Timing::Difference(_) => Timing::WaitFor(num_cycles), + }; + } + } + } else if out == "[ok]" { + if let Timing::Difference(cycles) = number_of_cycles { + println!( + "[ok: {} c ≈ {} s]", + cycles, + ((cycles as f64 / (16.78 * 1_000_000.0)) * 100.0).round() / 100.0 + ); + } else { + println!("{}", out); + } } else { println!("{}", out); } From 41bd8013f7b234ca27dcc15985941ba8d4d1d7e4 Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Sat, 3 Jul 2021 22:20:03 +0100 Subject: [PATCH 5/9] run tests in both debug and release mode --- .github/workflows/rust.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d187969..fb1a0a5 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -38,7 +38,10 @@ jobs: - name: Run Clippy on agb image converter working-directory: agb-image-converter run: cargo clippy --verbose - - name: Run Tests for agb + - name: Run Tests for agb in debug mode working-directory: agb run: cargo test --verbose + - name: Run Tests for agb in release mode + working-directory: agb + run: cargo test --verbose --release \ No newline at end of file From 5bc8a334099674eb57bcb007ba1e5b83a35657ba Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Sat, 3 Jul 2021 22:37:16 +0100 Subject: [PATCH 6/9] h not needed --- mgba-test-runner/add_cycles_register.patch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mgba-test-runner/add_cycles_register.patch b/mgba-test-runner/add_cycles_register.patch index 5e767fe..b16d6dd 100644 --- a/mgba-test-runner/add_cycles_register.patch +++ b/mgba-test-runner/add_cycles_register.patch @@ -20,7 +20,7 @@ index cc39e1192..d34dcb4b4 100644 return; + case REG_DEBUG_CYCLES: { + int32_t number_of_cycles = mTimingCurrentTime(&gba->timing); -+ mLOG(GBA_DEBUG, INFO, "Cycles: %d Tag: %hd", number_of_cycles, value); ++ mLOG(GBA_DEBUG, INFO, "Cycles: %d Tag: %d", number_of_cycles, value); + return; + } case REG_DEBUG_FLAGS: From c3da877c141c7c05ae73b5334bdd6d1288c774f7 Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Sat, 3 Jul 2021 22:53:49 +0100 Subject: [PATCH 7/9] install verbosely --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index fb1a0a5..f3e1173 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -28,7 +28,7 @@ jobs: key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - name: install mgba-test-runner - run: cargo install --path mgba-test-runner + run: cargo install --path mgba-test-runner --verbose - name: Build agb library working-directory: agb run: cargo build --verbose From 819d86cfbf42f37227236b72a66e94e473c4e709 Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Tue, 13 Jul 2021 22:24:08 +0100 Subject: [PATCH 8/9] debug mode build --- mgba-test-runner/Cargo.toml | 3 +++ mgba-test-runner/build-mgba.sh | 1 + mgba-test-runner/build.rs | 1 + 3 files changed, 5 insertions(+) diff --git a/mgba-test-runner/Cargo.toml b/mgba-test-runner/Cargo.toml index 855bdca..85e1271 100644 --- a/mgba-test-runner/Cargo.toml +++ b/mgba-test-runner/Cargo.toml @@ -6,6 +6,9 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[profile.release] +debug = true + [dependencies] regex = "1" anyhow = "1.0" diff --git a/mgba-test-runner/build-mgba.sh b/mgba-test-runner/build-mgba.sh index da488c5..05b9bb0 100644 --- a/mgba-test-runner/build-mgba.sh +++ b/mgba-test-runner/build-mgba.sh @@ -31,6 +31,7 @@ cmake .. \ -DUSE_LZMA=OFF \ -DUSE_DISCORD_RPC=OFF \ -DENABLE_SCRIPTING=OFF \ + -DCMAKE_BUILD_TYPE=Debug \ -DUSE_EPOXY=OFF make diff --git a/mgba-test-runner/build.rs b/mgba-test-runner/build.rs index 086e8b9..29d97b4 100644 --- a/mgba-test-runner/build.rs +++ b/mgba-test-runner/build.rs @@ -19,6 +19,7 @@ fn main() { .file("c/test-runner.c") .include(&mgba_directory.join("include")) .static_flag(true) + .debug(true) .compile("test-runner"); let bindings = bindgen::Builder::default() From 87fb32dfb0e252f5a1fb939385e0aea428f3f074 Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Tue, 13 Jul 2021 22:24:19 +0100 Subject: [PATCH 9/9] initialise to zero --- mgba-test-runner/c/test-runner.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mgba-test-runner/c/test-runner.c b/mgba-test-runner/c/test-runner.c index bdebaf0..6e50d42 100644 --- a/mgba-test-runner/c/test-runner.c +++ b/mgba-test-runner/c/test-runner.c @@ -19,7 +19,7 @@ struct MGBA { }; struct MGBA* new_runner(char* filename) { - struct MGBA* mgba = malloc(sizeof(struct MGBA)); + struct MGBA* mgba = calloc(1, sizeof(struct MGBA)); mgba->mlogger.log = log_output; mgba->callback.callback = NULL;