Skip to content

Feature/lua scripting#167

Merged
highbyte merged 57 commits into
masterfrom
feature/lua-scripting
Apr 7, 2026
Merged

Feature/lua scripting#167
highbyte merged 57 commits into
masterfrom
feature/lua-scripting

Conversation

@highbyte
Copy link
Copy Markdown
Owner

@highbyte highbyte commented Mar 6, 2026

Add Lua scripting support

Adds a Lua scripting engine to the emulator, exposed in the Avalonia Desktop and Browser host apps. Scripts run as coroutines alongside the emulator and can read/write CPU registers and memory, inject input, observe emulator state, and control the emulator lifecycle — enabling automation, TAS-style input playback, and in-emulator tooling without modifying the core.

Scripting model

Two styles can be mixed in a single script:

  • Linear loop (BizHawk-style): top-level while true do … emu.frameadvance() end — reads like a sequential program, suspends on each frame
  • Event hooks: global callbacks (on_before_frame(), on_started(), on_stopped(), etc.) called at specific emulator lifecycle points

Powered by MoonSharp, with AOT-compatible configuration for the WASM/Browser target.

Lua API surface

Domain Highlights
Emulator control emu.start/pause/stop/reset/select/quit(), emu.config_valid()
Queries emu.state(), emu.framecount(), emu.time(), emu.selected_system/variant()
CPU Read-only access to all registers and flags via cpu.*
Memory mem.read(addr) / mem.write(addr, val) — routes through the same address decoding as the emulated CPU
Input Keyboard and joystick state query + synthetic injection (input.key_press, input.joystick_set, etc.)
HTTP Async GET/POST via http.* (works in both Desktop and Browser)
File I/O file.* read/write with configurable base-directory sandboxing (Desktop only)
TCP client tcp.* (Desktop only, opt-in)
Store Cross-platform key/value persistence — filesystem on Desktop, localStorage in Browser

C64-specific integrations

  • Type text into BASIC directly from Lua
  • Load .d64 disk images at runtime

Host app integration

Desktop:

  • Scripts loaded from a configurable directory (appsettings.json)
  • --script <file> CLI argument to load and run a single script on startup (useful for CI/automation pipelines)
  • Scripts tab shows directory path with an "open in file manager" button and a refresh button

Browser:

  • In-app script editor (add, edit, delete) backed by localStorage
  • AOT-safe MoonSharp configuration
  • store API backed by localStorage instead of the filesystem

Scripts tab UI

Both apps show a Scripts tab with live status for all loaded scripts:

  • Status dot (green / red / grey), enable/disable toggle, per-script reload button
  • Columns: script name, status, yield type, registered hooks
  • Column headers are clickable to sort by any column (ascending/descending toggle)
  • Tab header shows active count; turns red when any script is system-disabled (syntax or runtime error)
  • Scripts that error during execution are automatically disabled and flagged

Configuration

All behaviour is controlled via appsettings.json under Highbyte.DotNet6502.Scripting: master enable switch, script directory, per-feature allow-flags (AllowFileIO, AllowHttpRequests, AllowTcpClient, AllowStore), execution warning threshold, and runaway-script instruction limit.

@highbyte highbyte force-pushed the feature/lua-scripting branch from 53909de to c532ef7 Compare April 1, 2026 13:12
highbyte added 29 commits April 7, 2026 13:46
Make script engine lifecycle start before emulator is started.
New  coroutine wait option "yield" not tied to emulator is generating frames.
…in a reasonable number of instructions, and will then log error and disable them.
…engine than MoonSharp will be used in the future.
… not tied to any specific Lua engine to Highbyte.DotNet6502.Systems library.
@highbyte highbyte force-pushed the feature/lua-scripting branch from f54e540 to 9cc9b35 Compare April 7, 2026 11:50
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Apr 7, 2026

@highbyte highbyte marked this pull request as ready for review April 7, 2026 11:56
@highbyte highbyte merged commit 655e9f8 into master Apr 7, 2026
5 checks passed
@highbyte highbyte deleted the feature/lua-scripting branch April 7, 2026 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant