Sunday, April 13, 2025

Link libraries script for MSX-2 Basic code

PowerShell Script: linklib.ps1

Script Requirements

  • The script must be named linklib.ps1.
  • Accept named parameters:
    • -FilePath: Full path to a .bas file (required)
    • -LibFolder: Optional path to a folder containing library files
    • -TargetFile: Optional output file path; if not provided, overwrite the original file
  • Process the input file line by line.
  • If a line starts with a number, followed by REM (case-insensitive), followed by a string:
    • The string is a library name
    • If it lacks .bas suffix, append it
    • Try to resolve it as a file path
    • If not found, try LibFolder + filename if LibFolder is provided
    • If still not found, terminate with an error
    • If found, replace the original line with contents of the library file
  • Use the actual script name in all output messages (using $MyInvocation.MyCommand.Name)

Script Code

<#
.SYNOPSIS
    Replaces library include lines in a .bas file with actual file content.

.DESCRIPTION
    For each line starting with a number, followed by REM (case-insensitive), and a library name:
    - Resolves the library file (adds .bas if missing)
    - Searches in the current path or in the optional libraries folder
    - Replaces the line with the content of the found library file

.PARAMETER FilePath
    The full path to the .bas file to process.

.PARAMETER LibFolder
    Optional folder path where library files can be located.

.PARAMETER TargetFile
    Optional path to write the result. If not provided, overwrites the source file.

.EXAMPLE
    .linklib.ps1 -FilePath "C:codemain.bas" -LibFolder "C:codelibs" -TargetFile "C:codelinked.bas"
#>

param (
    [Parameter(Mandatory = $true)]
    [string]$FilePath,

    [Parameter(Mandatory = $false)]
    [string]$LibFolder,

    [Parameter(Mandatory = $false)]
    [string]$TargetFile
)

$scriptName = $MyInvocation.MyCommand.Name

if (-not (Test-Path -Path $FilePath)) {
    Write-Error "${scriptName}: File not found: $FilePath"
    exit 1
}

if (-not $TargetFile) {
    $TargetFile = $FilePath
}

try {
    $lines = Get-Content -LiteralPath $FilePath
    $output = @()

    foreach ($line in $lines) {
        if ($line -match '^s*(d+)s+REMs+(.*)$') {
            $lineNumber = $matches[1]
            $libName = $matches[2].Trim()

            if (-not $libName.ToLower().EndsWith(".bas")) {
                $libName += ".bas"
            }

            $resolvedPath = $libName

            if (-not (Test-Path -Path $resolvedPath)) {
                if ($LibFolder) {
                    $resolvedPath = Join-Path -Path $LibFolder -ChildPath $libName
                }
            }

            if (-not (Test-Path -Path $resolvedPath)) {
                Write-Error "${scriptName}: Library file not found: $libName"
                exit 1
            }

            Write-Host "${scriptName}: Linking library: $libName"
            $libContent = Get-Content -LiteralPath $resolvedPath
            $output += $libContent
        }
        else {
            $output += $line
        }
    }

    Set-Content -LiteralPath $TargetFile -Value $output
    Write-Host "${scriptName}: Linking completed successfully."
    Write-Host "${scriptName}: Output written to: $TargetFile"
}
catch {
    Write-Error "${scriptName}: Error during processing - $_"
    exit 1
}

Saturday, April 12, 2025

Build labels resolving script for MSX Basic with chatGpt

<#
.SYNOPSIS
    Processes a text file by adding line numbers and replacing label-based GOTO/GOSUB commands.

.DESCRIPTION
    - Each line is numbered sequentially starting from a given Start, with an increment of Step.
    - Labels (lines starting with ':') are replaced with a numbered line containing "REM :label".
    - GOSUB and GOTO commands referencing a label (e.g., "gosub myLabel") are replaced with the numeric line.
    - Case-insensitive matching for labels and commands.

.PARAMETER FilePath
    Path to the file to process.

.PARAMETER Start
    Starting number for line numbering (default: 10).

.PARAMETER Step
    Increment step for line numbering (default: 10).

.EXAMPLE
    .RenumberWithLabels.ps1 -FilePath "C:codescript.bas" -Start 10 -Step 10
#>

param(
    [string]$FilePath,
    [int]$Start = 10,
    [int]$Step = 10
)

if (-not (Test-Path $FilePath)) {
    Write-Error "File not found: $FilePath"
    exit 1
}

$lines = Get-Content $FilePath
$output = @()
$labelMap = @{}
$lineNumber = $Start

# First pass: assign line numbers and store label mappings
foreach ($line in $lines) {
    $trimmed = $line.Trim()
    if ($trimmed -match '^:([a-zA-Z_][w]*)$') {
        $label = $matches[1]
        $labelMap[$label.ToLower()] = $lineNumber
        $output += "$lineNumber REM :$label"
    }
    else {
        $output += "$lineNumber $line"
    }
    $lineNumber += $Step
}

# Second pass: replace GOSUB/GOTO label references
for ($i = 0; $i -lt $output.Count; $i++) {
    $line = $output[$i]
    foreach ($label in $labelMap.Keys) {
        $line = $line -replace "(?i)bgosubs+:?$labelb", "gosub $($labelMap[$label])"
        $line = $line -replace "(?i)bgotos+:?$labelb", "GOTO $($labelMap[$label])"
    }
    $output[$i] = $line
}

# Save to file or print
$output | Set-Content "$FilePath.processed"
Write-Host "Processed file saved as: $FilePath.processed"

Friday, April 11, 2025

Create MSX-2 deploy script with AI. (Draft)

MSX-2 Disk Deployment Script Requirements

This document outlines the complete functionality for a PowerShell script that builds a bootable MSX-2 .dsk image from a BASIC project.

Input Parameters

  • MainBasPath (Required, String): Full path to the main .BAS file.
  • ProjectName (Optional, String): Optional project name. Defaults to the base name of the .BAS file.

Folder and File Structure

Work Folder

  • Location: Sibling to the folder containing MainBasPath.
  • Name Format: ProjectName_Hash (the hash is 6 characters from a GUID).
  • Contents: Renumbered .BAS files and AUTOEXEC.BAS.

Disk Image

  • Name: ProjectName.dsk.
  • Location: Same parent folder as the folder containing MainBasPath.
  • Base: Created by copying the "New 720KB Boot Disk.dsk" template.
  • Overwrite: If the file already exists, it is overwritten.

Steps

  1. Resolve and Print All Paths
    • Compute all paths (folders, tools, and output) at the top of the script and print them.
  2. Create Work Folder
    • Create a work folder as a sibling to the project folder using the format: ProjectName_Hash.
  3. Create AUTOEXEC.BAS
    • Create AUTOEXEC.BAS in the work folder.
    • The file content should be: RUN "MainFile.BAS" (using the actual main file name), written in ASCII.
  4. Copy and Renumber .BAS Files
    1. Copy all .BAS files from the folder containing MainBasPath to the work folder.
    2. Renumber each .BAS file by calling the external renumbering script:
      D:WorkspaceMSXtoolsrenumber.ps1 -FilePath -Step 10 -Start 10
  5. Create and Populate Disk Image
    1. Copy the template image "New 720KB Boot Disk.dsk" to the destination DiskImage path, overwriting any existing file.
    2. Add each file from the work folder to the disk image using the command:
      dsktool.exe A "C:pathtoimage.dsk" "C:pathtofile.ext"
    3. If adding a file fails, print the exact command used for troubleshooting.
  6. Cleanup or Error Handling
    1. If all files are added successfully, delete the work folder and print "Deployment Complete".
    2. If any file fails to add, do not delete the work folder and print "Deployment Failed" along with the error information and the failed command.

Additional Notes

  • All paths are fully resolved and printed before any processing begins.
  • Original file casing is preserved in the disk image.
  • The final disk image will overwrite any existing file with the same name.
  • Failures in file addition are clearly reported along with the command used.