From 87abbaa75eea6e1d5bcc5acd9c3afc9ba847e153 Mon Sep 17 00:00:00 2001 From: Adrian Conlon <98398945+AdrianConlon@users.noreply.github.com> Date: Fri, 28 Mar 2025 14:50:53 +0000 Subject: [PATCH] Tidy IO page access --- LR35902/IoRegisters.cs | 8 ++++++-- LR35902/LR35902.cs | 14 ++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/LR35902/IoRegisters.cs b/LR35902/IoRegisters.cs index 27d0c32..d397cf4 100644 --- a/LR35902/IoRegisters.cs +++ b/LR35902/IoRegisters.cs @@ -8,7 +8,11 @@ namespace LR35902 public sealed class IoRegisters : Ram { - public const int BASE = 0xFF00; + public const byte BasePage = 0xFF; + + private static readonly ushort _base = Chip.PromoteByte(BasePage); + + public static ushort BASE => _base; // Port/Mode Registers public const int P1 = 0x0; // R/W Mask5 @@ -343,7 +347,7 @@ namespace LR35902 private void Bus_ReadingByte(object? sender, EventArgs e) { var address = this.bus.Address.Word; - var io = address is >= BASE and < 0xff80; + var io = address >= BASE && address < 0xff80; if (io) { var port = (ushort)(address - BASE); diff --git a/LR35902/LR35902.cs b/LR35902/LR35902.cs index b4883b1..abfcdea 100644 --- a/LR35902/LR35902.cs +++ b/LR35902/LR35902.cs @@ -45,10 +45,12 @@ namespace LR35902 public bool IME { get; set; } + private static readonly Register16 _addressIE = new(IoRegisters.IE, IoRegisters.BasePage); + public byte IE { - get => this.Bus.Peek(IoRegisters.BASE + IoRegisters.IE); - set => this.Bus.Poke(IoRegisters.BASE + IoRegisters.IE, value); + get => this.Bus.Peek(_addressIE); + set => this.Bus.Poke(_addressIE, value); } public byte IF @@ -689,7 +691,7 @@ namespace LR35902 break; case 4: // GB: LD (FF00 + n),A - this.MemoryWrite((ushort)(IoRegisters.BASE + this.FetchByte()), this.A); + this.MemoryWrite(this.FetchByte(), IoRegisters.BasePage, this.A); break; case 5: @@ -707,7 +709,7 @@ namespace LR35902 break; case 6: // GB: LD A,(FF00 + n) - this.A = this.MemoryRead((ushort)(IoRegisters.BASE + this.FetchByte())); + this.A = this.MemoryRead(this.FetchByte(), IoRegisters.BasePage); break; case 7: @@ -772,7 +774,7 @@ namespace LR35902 _ = this.JumpConditionalFlag(y); break; case 4: // GB: LD (FF00 + C),A - this.MemoryWrite((ushort)(IoRegisters.BASE + this.C), this.A); + this.MemoryWrite(this.C, IoRegisters.BasePage, this.A); break; case 5: // GB: LD (nn),A this.MEMPTR.Assign(this.FetchWord()); @@ -780,7 +782,7 @@ namespace LR35902 this.MemoryWrite(this.A); break; case 6: // GB: LD A,(FF00 + C) - this.A = this.MemoryRead((ushort)(IoRegisters.BASE + this.C)); + this.A = this.MemoryRead(this.C, IoRegisters.BasePage); break; case 7: // GB: LD A,(nn) this.MEMPTR.Assign(this.FetchWord());