Skip to content

Add lua event listener for CPU::Interrupt and scheduleInterrupt#1996

Open
johnbaumann wants to merge 4 commits intogrumpycoders:mainfrom
johnbaumann:lua-cycle-callback
Open

Add lua event listener for CPU::Interrupt and scheduleInterrupt#1996
johnbaumann wants to merge 4 commits intogrumpycoders:mainfrom
johnbaumann:lua-cycle-callback

Conversation

@johnbaumann
Copy link
Copy Markdown
Collaborator

Adding a method for scheduling callbacks into Lua based on elapsed CPU cycles. Builds on existing scheduler and event listener methods.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 4, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b147b097-6141-46fe-8121-5d253397dfdd

📥 Commits

Reviewing files that changed from the base of the PR and between 02c843f and 1763832.

📒 Files selected for processing (1)
  • src/core/pcsxffi.lua

📝 Walkthrough

Walkthrough

Adds Lua-driven CPU interrupt support: new PSXINT_LUA interrupt and scale, Lua-exposed scheduleInterrupt(eCycle) via FFI and C++ binding, PCSX::Lua::interrupt() that emits Events::CPU::Interrupt, and event dispatch support for the "CPU::Interrupt" name.

Changes

Cohort / File(s) Summary
Event Payload
src/core/system.h
Added PCSX::Events::CPU::Interrupt payload type to the events API.
CPU Interrupt Core
src/core/r3000a.h, src/core/r3000a.cc
Added PSXINT_LUA enumerator; expanded m_interruptScales array size; included PSXINT_LUA check in branchTest() interrupt handling.
Lua ↔ CPU API
src/core/pcsxffi.lua, src/core/pcsxlua.cc
Declared FFI scheduleInterrupt(uint32_t) and Lua wrapper with validation; added C++ PCSX.scheduleInterrupt(eCycle) binding that forwards to CPU scheduler.
Lua Signaling & Dispatch
src/lua/luawrapper.h, src/lua/luawrapper.cc, src/core/eventslua.cc
Added PCSX::Lua::interrupt() method that emits Events::CPU::Interrupt via the event bus; extended event name dispatch to handle "CPU::Interrupt".

Sequence Diagram

sequenceDiagram
    participant LuaScript as Lua Script
    participant LuaFFI as Lua FFI
    participant CppBridge as C++ Binding
    participant CPU as R3000A CPU
    participant EventBus as Event Bus

    LuaScript->>LuaFFI: scheduleInterrupt(eCycle)
    LuaFFI->>CppBridge: validated eCycle
    CppBridge->>CPU: scheduleInterrupt(PSXINT_LUA, eCycle)
    CPU->>CPU: record target cycle for PSXINT_LUA
    CPU->>EventBus: when firing -> emit Events::CPU::Interrupt {}
    EventBus->>LuaScript: dispatch "CPU::Interrupt" to listeners
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 I nudged a cycle, soft and bright,

Lua whispered, "Wake the CPU tonight,"
A tiny interrupt, a hop, a ring,
Events unfurl — the listeners sing,
A rabbit's tap, emulator delight.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main changes: adding a Lua event listener for CPU::Interrupt and implementing the scheduleInterrupt method across multiple files.
Description check ✅ Passed The description is related to the changeset, mentioning scheduling callbacks into Lua based on CPU cycles, which aligns with the event listener and scheduleInterrupt functionality added.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/core/pcsxffi.lua`:
- Around line 176-179: The scheduleInterrupt function currently accepts any Lua
number; change its validation to ensure eCycle is an integer within uint32 range
before calling C.scheduleInterrupt: verify type(eCycle) == 'number', confirm
it's an integer (e.g. eCycle == math.floor(eCycle)), and enforce 0 <= eCycle <=
0xFFFFFFFF, otherwise raise an error with a clear message; then pass the
validated value to C.scheduleInterrupt to avoid undefined FFI conversions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 551567ca-5460-4ec3-8fa0-8a3a424c91f3

📥 Commits

Reviewing files that changed from the base of the PR and between c2ad3b2 and 0533edb.

📒 Files selected for processing (8)
  • src/core/eventslua.cc
  • src/core/pcsxffi.lua
  • src/core/pcsxlua.cc
  • src/core/r3000a.cc
  • src/core/r3000a.h
  • src/core/system.h
  • src/lua/luawrapper.cc
  • src/lua/luawrapper.h

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
src/core/pcsxffi.lua (1)

176-179: ⚠️ Potential issue | 🟠 Major

eCycle still allows fractional/NaN inputs before uint32_t FFI conversion.

Line 177-178 checks type and range only. Values like 1.5 and 0/0 can slip through and produce truncation/undefined conversion behavior at the FFI boundary.

🔧 Proposed fix
 local function scheduleInterrupt(eCycle)
     if type(eCycle) ~= 'number' then error 'PCSX.scheduleInterrupt requires a numeric cycle count' end
-    if (eCycle < 0) or (eCycle > 0xffffffff) then
-        error 'PCSX.scheduleInterrupt cycle count parameter should be an unsigned 32-bit integer'
+    if (eCycle ~= eCycle) or (eCycle < 0) or (eCycle > 0xffffffff) or (eCycle % 1 ~= 0) then
+        error 'PCSX.scheduleInterrupt cycle count parameter should be a finite unsigned 32-bit integer'
     end
     C.scheduleInterrupt(eCycle)
 end
#!/bin/bash
# Verify integer/NaN guards are present (or absent) in scheduleInterrupt validation.
rg -n "local function scheduleInterrupt|eCycle ~= eCycle|eCycle % 1|0xffffffff|C.scheduleInterrupt" src/core/pcsxffi.lua -A2 -B2
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/core/pcsxffi.lua` around lines 176 - 179, The scheduleInterrupt function
currently only checks type and range, allowing fractional or NaN values to reach
the FFI call; update local function scheduleInterrupt(eCycle) to reject
non-integers and NaN by adding explicit checks (e.g., ensure eCycle == eCycle to
reject NaN and eCycle % 1 == 0 or math.floor(eCycle) == eCycle to enforce
integer) before the existing 0..0xffffffff range check, then call
C.scheduleInterrupt only after these validations succeed so the FFI receives a
valid unsigned 32-bit integer.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/core/pcsxffi.lua`:
- Around line 176-180: The function scheduleInterrupt contains an unclosed if:
after checking (eCycle < 0) or (eCycle > 0xffffffff) you must add a closing end
before calling C.scheduleInterrupt; update the scheduleInterrupt function so the
numeric-range if block is properly terminated (i.e., insert the missing end to
close the if that validates eCycle) so the function reads: validate type,
validate range (with its own end), then call C.scheduleInterrupt(eCycle).

---

Duplicate comments:
In `@src/core/pcsxffi.lua`:
- Around line 176-179: The scheduleInterrupt function currently only checks type
and range, allowing fractional or NaN values to reach the FFI call; update local
function scheduleInterrupt(eCycle) to reject non-integers and NaN by adding
explicit checks (e.g., ensure eCycle == eCycle to reject NaN and eCycle % 1 == 0
or math.floor(eCycle) == eCycle to enforce integer) before the existing
0..0xffffffff range check, then call C.scheduleInterrupt only after these
validations succeed so the FFI receives a valid unsigned 32-bit integer.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b7f03e25-8656-42c2-aee9-223d640b75c4

📥 Commits

Reviewing files that changed from the base of the PR and between 0533edb and 02c843f.

📒 Files selected for processing (1)
  • src/core/pcsxffi.lua

Comment thread src/core/pcsxffi.lua
Comment on lines +176 to +180
local function scheduleInterrupt(eCycle)
if type(eCycle) ~= 'number' then error 'PCSX.scheduleInterrupt requires a numeric cycle count' end
if (eCycle < 0) or (eCycle > 0xffffffff) then error 'PCSX.scheduleInterrupt cycle count parameter should be an unsigned 32-bit integer'
C.scheduleInterrupt(eCycle)
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify the exact changed block and confirm missing `end` on the second if.
sed -n '176,181p' src/core/pcsxffi.lua
python - <<'PY'
from pathlib import Path
line178 = Path("src/core/pcsxffi.lua").read_text().splitlines()[177]  # 1-based line 178
print("Line 178:", line178)
print("Ends with 'end':", line178.rstrip().endswith("end"))
PY

Repository: grumpycoders/pcsx-redux

Length of output: 560


Add missing end to close the if block on line 178.

Line 178 opens an if statement but does not close it, creating a syntax error that prevents the Lua chunk from loading.

🔧 Proposed fix
 local function scheduleInterrupt(eCycle)
     if type(eCycle) ~= 'number' then error 'PCSX.scheduleInterrupt requires a numeric cycle count' end
-    if (eCycle < 0) or (eCycle > 0xffffffff) then error 'PCSX.scheduleInterrupt cycle count parameter should be an unsigned 32-bit integer'
+    if (eCycle < 0) or (eCycle > 0xffffffff) then
+        error 'PCSX.scheduleInterrupt cycle count parameter should be an unsigned 32-bit integer'
+    end
     C.scheduleInterrupt(eCycle)
 end
🧰 Tools
🪛 Luacheck (1.2.0)

[error] 179-179: expected 'end' (to close 'if' on line 178) near 'C' (indentation-based guess)

(E011)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/core/pcsxffi.lua` around lines 176 - 180, The function scheduleInterrupt
contains an unclosed if: after checking (eCycle < 0) or (eCycle > 0xffffffff)
you must add a closing end before calling C.scheduleInterrupt; update the
scheduleInterrupt function so the numeric-range if block is properly terminated
(i.e., insert the missing end to close the if that validates eCycle) so the
function reads: validate type, validate range (with its own end), then call
C.scheduleInterrupt(eCycle).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant