From 317d22b8abf65ab2fcf79ac1498310131096bbc0 Mon Sep 17 00:00:00 2001 From: MadMaurice Date: Wed, 30 Aug 2023 13:02:20 +0200 Subject: [PATCH] memory/bus - use Range class --- memory/bus.cpp | 12 ++++++------ memory/bus.h | 3 +-- memory/mem_device.h | 3 +-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/memory/bus.cpp b/memory/bus.cpp index 50d1033..2628322 100644 --- a/memory/bus.cpp +++ b/memory/bus.cpp @@ -6,7 +6,7 @@ std::optional Bus::find_entry(u16 addr) { for(auto it = map.begin(); it != map.end(); ++it) { - if (addr >= it->start && addr <= it->end) + if (it->range.contains(addr)) return std::make_optional(*it); } @@ -15,7 +15,7 @@ std::optional Bus::find_entry(u16 addr) void Bus::map_device(u16 start, u16 end, Mem_device* dev) { - map.push_back(Bus::MapEntry{ start, end, dev }); + map.push_back(Bus::MapEntry{ Range(start,end), dev }); } void Bus::write8(u16 addr, u8 data) @@ -24,7 +24,7 @@ void Bus::write8(u16 addr, u8 data) if(!mapentry) return; - mapentry->dev->write8(addr - mapentry->start, data); + mapentry->dev->write8(addr - mapentry->range.start, data); } u8 Bus::read8(u16 addr) @@ -33,19 +33,19 @@ u8 Bus::read8(u16 addr) if(!mapentry) return 0xFFu; - return mapentry->dev->read8(addr - mapentry->start); + return mapentry->dev->read8(addr - mapentry->range.start); } void Bus::write16(u16 addr, u16 data) { auto mapentry = find_entry(addr); if(!mapentry) return; - mapentry->dev->write16(addr - mapentry->start, data); + mapentry->dev->write16(addr - mapentry->range.start, data); } u16 Bus::read16(u16 addr) { auto mapentry = find_entry(addr); if(!mapentry) return 0xFFFFu; - return mapentry->dev->read16(addr - mapentry->start); + return mapentry->dev->read16(addr - mapentry->range.start); } diff --git a/memory/bus.h b/memory/bus.h index c50bcaa..b7709a3 100644 --- a/memory/bus.h +++ b/memory/bus.h @@ -8,8 +8,7 @@ class Bus : public Mem_device { private: struct MapEntry { - u16 start; - u16 end; + Range range; Mem_device *dev; }; diff --git a/memory/mem_device.h b/memory/mem_device.h index a72b994..8f031b4 100644 --- a/memory/mem_device.h +++ b/memory/mem_device.h @@ -3,11 +3,10 @@ #include class Range { -private: +public: u16 start; u16 end; -public: constexpr Range(u16 start, u16 end) : start(start), end(end) {};