I'm trying to implement the LC-3 architecture in c++. I found a tutorial in C.
I have all the code I need. I built the source code ran the executable give it an obj file who contain the software to run but I'm stuck in the first instruction of the obj file. This is the code of the first instruction execution:
case Registry::OP_LD:
{
uint16_t r0 = (instr >> 9) & 0x7;
uint16_t pc_offset = m_registry->sign_extend(instr & 0x1FF, 9);
printf("m_memory: %p\n", m_memory);
printf("m_registry: %p\n", m_registry);
printf("\n\n\n\n\n\n");
printf("m_memory[0]: %d\n", m_memory->read(0));
printf("m_registry[0]: %d\n", m_registry->get(0));
printf("\n\n\n\n\n\n");
printf("R0: %d\n", r0);
printf("pc_offset: %d\n", pc_offset);
printf("Reg 0: %d\n", m_registry->get(r0));
printf("mem read: %d\n", m_memory->read(m_registry->get(static_cast<uint16_t>(Registry::R_PC)) + pc_offset));
m_registry->set(r0, m_memory->read(m_registry->get(static_cast<uint16_t>(Registry::R_PC)) + pc_offset));
printf("Reg 0 bis: %d\n", m_registry->get(r0));
m_registry->update_flags(r0);
}
I tried to debug this by putting printf to saw what's happened. I save the result in test (check the archive). I extract a result here as an example:
m_memory: 0x7f5c20645010
m_registry: 0x558784821ef0
m_memory[0]: 0
m_registry[0]: 0
R0: 6
pc_offset: 23
Reg 0: 61477
mem read: 61477
Reg 0 bis: 61477
it's seems, for an unknown reason, the reg and/or the memory it's not set correctly. I don't really know how to debug this. If someone can help me to debug this it's would be great.
The source code is here archive
Cheers