diff --git a/include/memory.hh b/include/memory.hh index 5b2f931..b2bf34f 100644 --- a/include/memory.hh +++ b/include/memory.hh @@ -17,12 +17,6 @@ class Memory { uint8_t read(size_t address) const; void write(size_t address, uint8_t byte); - uint16_t read_halfword(size_t address) const; - void write_halfword(size_t address, uint16_t halfword); - - uint32_t read_word(size_t address) const; - void write_word(size_t address, uint32_t word); - private: #define MEMORY_REGION(name, start, end) \ static constexpr size_t name##_START = start; \ diff --git a/src/bus.cc b/src/bus.cc index 93ca7a5..5349d42 100644 --- a/src/bus.cc +++ b/src/bus.cc @@ -1,4 +1,5 @@ #include "bus.hh" +#include "util/log.hh" #include namespace matar { @@ -17,21 +18,38 @@ Bus::write_byte(size_t address, uint8_t byte) { uint16_t Bus::read_halfword(size_t address) { - return memory->read_halfword(address); + if (address & 0b01) + glogger.warn("Reading a non aligned halfword address"); + + return memory->read(address) | memory->read(address + 1) << 8; } void Bus::write_halfword(size_t address, uint16_t halfword) { - memory->write_halfword(address, halfword); + if (address & 0b01) + glogger.warn("Writing to a non aligned halfword address"); + + memory->write(address, halfword & 0xFF); + memory->write(address + 1, halfword >> 8 & 0xFF); } uint32_t Bus::read_word(size_t address) { - return memory->read_word(address); + if (address & 0b11) + glogger.warn("Reading a non aligned word address"); + + return memory->read(address) | memory->read(address + 1) << 8 | + memory->read(address + 2) << 16 | memory->read(address + 3) << 24; } void Bus::write_word(size_t address, uint32_t word) { - memory->write_word(address, word); + if (address & 0b11) + glogger.warn("Writing to a non aligned word address"); + + memory->write(address, word & 0xFF); + memory->write(address + 1, word >> 8 & 0xFF); + memory->write(address + 2, word >> 16 & 0xFF); + memory->write(address + 3, word >> 24 & 0xFF); } } diff --git a/src/memory.cc b/src/memory.cc index 98ac91c..3733ace 100644 --- a/src/memory.cc +++ b/src/memory.cc @@ -89,43 +89,6 @@ Memory::write(size_t address, uint8_t byte) { #undef MATCHES -uint16_t -Memory::read_halfword(size_t address) const { - if (address & 0b01) - glogger.warn("Reading a non aligned halfword address"); - - return read(address) | read(address + 1) << 8; -} - -void -Memory::write_halfword(size_t address, uint16_t halfword) { - if (address & 0b01) - glogger.warn("Writing to a non aligned halfword address"); - - write(address, halfword & 0xFF); - write(address + 1, halfword >> 8 & 0xFF); -} - -uint32_t -Memory::read_word(size_t address) const { - if (address & 0b11) - glogger.warn("Reading a non aligned word address"); - - return read(address) | read(address + 1) << 8 | read(address + 2) << 16 | - read(address + 3) << 24; -} - -void -Memory::write_word(size_t address, uint32_t word) { - if (address & 0b11) - glogger.warn("Writing to a non aligned word address"); - - write(address, word & 0xFF); - write(address + 1, word >> 8 & 0xFF); - write(address + 2, word >> 16 & 0xFF); - write(address + 3, word >> 24 & 0xFF); -} - void Memory::parse_header() {