Skip to content

Instantly share code, notes, and snippets.

@svofski
Created April 3, 2023 18:05
Show Gist options
  • Save svofski/c67c2aad83097f6b18515b968c8f3c30 to your computer and use it in GitHub Desktop.
Save svofski/c67c2aad83097f6b18515b968c8f3c30 to your computer and use it in GitHub Desktop.
in spite of common sense, bas2cas for basic 2.5 bload (vector-06c)
@echo off
setlocal enabledelayedexpansion
set input=%1
set output=%2
set startaddr=%3
set inputfile=%~dp0%~1
set outputfile=%~dp0%~2
echo Convert binary file to BLOAD-formatted .CAS for BASIC 2.5
if "%3" == "" (
goto :help
)
if "%2" == "" (
goto :help
)
if "%1" == "" (
goto :help
)
echo Input file: %inputfile%
echo Output file: %outputfile%
cd %TMP%
del /q/s bin2cas >nul 2>&1
mkdir bin2cas >nul 2>&1
cd bin2cas
call :bin2hex %inputfile% hexbytes.txt
rem calculate checksum and length
set /a sum=0
set /a len=0
for /f %%a in (hexbytes.txt) do (
set /a value="0x%%a"
set /a sum=sum + value
set /a len=len + 1
)
set /a checksum=sum %% 256
echo Length: %len%
set /a startmsb=startaddr / 256
set /a startlsb=startaddr %% 256
set /a endaddr=startaddr + len - 1
set /a endmsb=endaddr / 256
set /a endlsb=endaddr %% 256
rem generate header
echo d2 d2 d2 d2 >outhex.txt
set /p=%input%<nul >tmp.txt
call :bin2hex tmp.txt namehex.txt
type namehex.txt >>outhex.txt
echo 00 00 00 >> outhex.txt
for /l %%i in (0,1,255) do echo 00 >>outhex.txt
echo e6 >> outhex.txt
call :dec2hex %startmsb% hex
echo %hex% >> outhex.txt
call :dec2hex %startlsb% hex
echo %hex% >> outhex.txt
call :dec2hex %endmsb% hex
echo %hex% >> outhex.txt
call :dec2hex %endlsb% hex
echo %hex% >> outhex.txt
type hexbytes.txt >> outhex.txt
call :dec2hex %checksum% hex
echo %hex% >> outhex.txt
certutil -f -decodehex outhex.txt %outputfile% >nul
exit
:dec2hex decimalVariable hexVariable
setlocal
set /a "decimal=%1"
set /a "high=((decimal & 0xf0) >> 4) + 1"
set /a "low=(decimal & 0x0f) + 1"
set hexchars="0123456789abcdef"
set "high=!hexchars:~%high%,1!"
set "low=!hexchars:~%low%,1!"
set "hex=%high%%low%"
endlocal & set "%~2=%hex%"
exit /b
:bin2hex
rem convert bin to hex
certutil -f -encodehex %~1 %~1.tmp >nul
set /p=""<nul >%~2
rem remove extra fluff from the hex file
for /f "tokens=*" %%a in (%~1.tmp) do (
rem echo "Line=%%a"
for %%b in (%%a) do (
echo %%b | findstr /r "\<[0-9a-f][0-9a-f]\>"
)
) >>%~2
exit /b
:help
echo Example usage:
echo bin2cas.bat bloadable.bin bloadable.cas 0x7000
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment