From 29fa05922385dfdf3270b06658e51498d9cc26dd Mon Sep 17 00:00:00 2001 From: DualFroz Date: Sun, 23 Aug 2026 12:36:38 +0200 Subject: [PATCH] feat(core): add system bus interface and interrupt controller --- src/GameBoy.Core/Cpu/InterruptController.cs | 50 +++++++++++++++++++++ src/GameBoy.Core/ISystemBus.cs | 12 +++++ 2 files changed, 62 insertions(+) create mode 100644 src/GameBoy.Core/Cpu/InterruptController.cs create mode 100644 src/GameBoy.Core/ISystemBus.cs diff --git a/src/GameBoy.Core/Cpu/InterruptController.cs b/src/GameBoy.Core/Cpu/InterruptController.cs new file mode 100644 index 0000000..ab718fe --- /dev/null +++ b/src/GameBoy.Core/Cpu/InterruptController.cs @@ -0,0 +1,50 @@ +using System.IO; + +namespace GameBoy.Cpu +{ + public enum Interrupt + { + VBlank = 0x01, + LcdStat = 0x02, + Timer = 0x04, + Serial = 0x08, + Joypad = 0x10 + } + + public sealed class InterruptController + { + public byte Enable; + public byte Flag; + + public byte ReadFlag() + { + return (byte)(Flag | 0xE0); + } + + public void WriteFlag(byte value) + { + Flag = (byte)(value & 0x1F); + } + + public void Request(Interrupt which) + { + Flag |= (byte)which; + } + + public byte Pending => (byte)(Enable & Flag & 0x1F); + + public bool AnyPending => Pending != 0; + + public void SaveState(BinaryWriter writer) + { + writer.Write(Enable); + writer.Write(Flag); + } + + public void LoadState(BinaryReader reader) + { + Enable = reader.ReadByte(); + Flag = reader.ReadByte(); + } + } +} diff --git a/src/GameBoy.Core/ISystemBus.cs b/src/GameBoy.Core/ISystemBus.cs new file mode 100644 index 0000000..c3274e8 --- /dev/null +++ b/src/GameBoy.Core/ISystemBus.cs @@ -0,0 +1,12 @@ +using GameBoy.Cpu; + +namespace GameBoy +{ + public interface ISystemBus + { + byte Read(ushort address); + void Write(ushort address, byte value); + void Tick4(); + InterruptController Interrupts { get; } + } +}