My issue is simple:
uint32_t memory::read_word1 (uint32_t address) {
if(address>(maxWord)){
return 0;
}
uint32_t temp = 10;
return temp;
}
uint32_t memory::read_word2 (uint32_t address) {
if(address>(maxWord)){
return 0;
}
return mem[address];
}
void memory::show_address (uint32_t address) {
int temp=read_word(address);
printf("%08jx\n", (uintmax_t)temp);
}
// Write a word of data to an address, mask contains 1s for bytes to be updated
void memory::write_word (uint32_t address, uint32_t data, uint32_t mask) {
if(address>(maxWord)){
int newMax = ceil(address/1024)*1024;
for(int i=maxWord+1;i<=newMax;i++){
mem.push_back(0);
}
maxWord=newMax;
}
data=data&mask;
mem[address]=data&~mask;
mem[address]=mem[address]|data;
}
read_word1 and 2 return the value to be printed through show_address. read_word2 is the function that we desire. However, the problem we find is that when we return from the vector using mem[address]
; read_word1 will print 0000000a
while read_word2 prints 00000010
when we set an element to 10 (via write_word).
To solve this I have tried using mem.at(address), type casting and even converting to string, all to no avail. The vector mem
is defined as
std::vector<uint32_t> mem=decltype(mem)(1024,0);
in the class header file.
It seems this is an underlying problem with the vector- what can be done?
EDIT:
Thanks everybody for your help. Feeling like the topic has been debunked a bit more. I couldn't get it working the way I thought I could, however if you pass a number in hex eg. 000000aa into write_address everything seems to work! All a bit strange, but thanks.