memory/bus - use Range class

This commit is contained in:
madmaurice 2023-08-30 13:02:20 +02:00
parent 66c98f53fb
commit 317d22b8ab
3 changed files with 8 additions and 10 deletions

View file

@ -6,7 +6,7 @@ std::optional<Bus::MapEntry> 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::MapEntry> 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);
}

View file

@ -8,8 +8,7 @@
class Bus : public Mem_device {
private:
struct MapEntry {
u16 start;
u16 end;
Range range;
Mem_device *dev;
};

View file

@ -3,11 +3,10 @@
#include <misc/types.h>
class Range {
private:
public:
u16 start;
u16 end;
public:
constexpr Range(u16 start, u16 end)
: start(start), end(end)
{};