@echo off
setlocal EnableExtensions EnableDelayedExpansion

REM v120: sidecar-backed packet matrix probe.
REM Runs locked/unlocked READ CAPACITY + MODE SENSE 3E and all captured
REM READ DVD STRUCTURE payloads from the .xdvd.json sidecar.  Uses the v118
REM centralized GDR hardware timer/status model and v119 post-timer main-loop
REM stop at 0x90006608.
REM
REM Usage:
REM   run_gdr8050l_auth_packet_v120_sidecar_matrix.cmd D:\path\game.xdvd.json [D:\path\game.iso]

if "%~1"=="" (
  echo Usage: %~nx0 D:\path\game.xdvd.json [D:\path\game.iso]
  exit /b 2
)

set "XDVD_JSON=%~1"
set "ISO_FLAGS="
if not "%~2"=="" set "ISO_FLAGS=--iso "%~2""

set "WORKSPACE=D:\vscode-workspace\xbox-dvd-tools-v2"
set "FWBIN=D:\vscode-workspace\xbox-dvd-tools-v2\firmware\GDR-8050L\8050L_fw_plaintext_256k.bin"

echo [V120] XDVD sidecar: %XDVD_JSON%
if not "%~2"=="" echo [V120] ISO metadata: %~2
echo [V120] Sidecar packet matrix: locked/unlocked capacity + mode page + AD/PFI/DMI.

call :run_probe locked_read_capacity "GDR-8050L v120 locked READ CAPACITY post-timer" read-capacity-10 locked 90000 || exit /b 1
call :run_probe locked_mode_sense_3e "GDR-8050L v120 locked MODE SENSE 3E post-timer" mode-sense-auth-xbox locked 90000 || exit /b 1
call :run_probe unlocked_read_capacity "GDR-8050L v120 unlocked READ CAPACITY post-timer" read-capacity-10 unlocked 90000 || exit /b 1
call :run_probe unlocked_mode_sense_3e "GDR-8050L v120 unlocked MODE SENSE 3E post-timer" mode-sense-auth-xbox unlocked 90000 || exit /b 1
call :run_probe read_dvd_structure_ad_c0 "GDR-8050L v120 READ DVD STRUCTURE AD C0 post-timer" read-dvd-structure-xbox locked 120000 || exit /b 1
call :run_probe read_dvd_structure_pfi "GDR-8050L v120 READ DVD STRUCTURE PFI post-timer" read-dvd-structure-pfi locked 120000 || exit /b 1
call :run_probe read_dvd_structure_dmi "GDR-8050L v120 READ DVD STRUCTURE DMI post-timer" read-dvd-structure-dmi locked 120000 || exit /b 1

echo [OK] v120 sidecar packet matrix complete.
echo [NEXT] Zip/send gdr8050l_auth_v120_*output.txt, *_summary.md, *_handshake.json, *_verify.md, and *_payload_reconstructed.bin.
exit /b 0

:run_probe
set "TAG=%~1"
set "TITLE=%~2"
set "CDB=%~3"
set "STATE=%~4"
set "MAXC=%~5"

echo.
echo [V120] Probe: !TAG! CDB=!CDB! state=!STATE!

python run_direct_firmware.py ^
  --workspace "%WORKSPACE%" ^
  --bin "%FWBIN%" ^
  --base 0x90000000 ^
  --entry 0x90000020 ^
  --model GDR-8050L ^
  --name "!TITLE!" ^
  --max-cycles !MAXC! ^
  --sp 0x003FF000 ^
  --preset gdr8050l-packet-full-cdb ^
  --packet-cdb !CDB! ^
  --xdvd-sidecar "%XDVD_JSON%" ^
  --xdvd-state !STATE! ^
  %ISO_FLAGS% ^
  --packet-data-in-source-mode stream ^
  --packet-data-in-source-no-prelude ^
  --packet-data-in-model-match-len 8 ^
  --gdr-hw-model ^
  --clear-preset-stops ^
  --stop-pc 0x90006608 ^
  --stop-pc-hit-count 4 ^
  --stop-pc-min-cycle 51200 ^
  --auth-watch ^
  --trace-csv gdr8050l_auth_v120_!TAG!_post_timer_trace.csv ^
  --trace-summary ^
  --config-out gdr8050l_auth_v120_!TAG!_post_timer_config.json ^
  > gdr8050l_auth_v120_!TAG!_post_timer.output.txt 2^>^&1

if errorlevel 1 goto fail_run

python analyze_gdr8050l_v109_readcap_route.py ^
  gdr8050l_auth_v120_!TAG!_post_timer_trace.csv ^
  --out gdr8050l_auth_v120_!TAG!_post_timer_summary.md

python extract_gdr_handshake.py ^
  gdr8050l_auth_v120_!TAG!_post_timer_trace.csv ^
  --out-json gdr8050l_auth_v120_!TAG!_post_timer_handshake.json ^
  --config gdr8050l_auth_v120_!TAG!_post_timer_config.json ^
  --out-df70 gdr8050l_auth_v120_!TAG!_post_timer_df70_all.bin ^
  --out-df70-after-packet gdr8050l_auth_v120_!TAG!_post_timer_df70_after_packet.bin

python analyze_gdr_chunked_data_in.py ^
  gdr8050l_auth_v120_!TAG!_post_timer_trace.csv ^
  --config gdr8050l_auth_v120_!TAG!_post_timer_config.json ^
  --out gdr8050l_auth_v120_!TAG!_post_timer_verify.md ^
  --out-payload gdr8050l_auth_v120_!TAG!_post_timer_payload_reconstructed.bin

if errorlevel 1 goto fail_verify

echo [OK] !TAG! passed.
exit /b 0

:fail_run
echo [FAIL] v120 run failed for !TAG!. Send gdr8050l_auth_v120_!TAG!_post_timer.output.txt
exit /b 1

:fail_verify
echo [FAIL] v120 payload verification failed for !TAG!. Send gdr8050l_auth_v120_!TAG!_post_timer_verify.md
exit /b 1
