59 lines
2.1 KiB
C
59 lines
2.1 KiB
C
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <inttypes.h>
|
|
#include <string.h>
|
|
#include "print.h"
|
|
#include "emulator.h"
|
|
|
|
#define UNSET_CONDITION_CODE_CHAR '-'
|
|
|
|
// Prints the current machine state into the provided stream
|
|
void printState(Machine *state, FILE *stream) {
|
|
printRegisters(state, stream);
|
|
printMemory(state, stream);
|
|
}
|
|
|
|
// Prints the current machine registers into the provided stream
|
|
void printRegisters(Machine *state, FILE *stream) {
|
|
fprintf(stream, "Registers:\n");
|
|
for (int i = 0; i < REGISTER_COUNT; i++) {
|
|
fprintf(stream, "X%02d\t= %016" PRIx64 "\n", i, state->registers[i]);
|
|
}
|
|
fprintf(stream, "PC\t= %016" PRIx64 "\n", state->pc);
|
|
fprintf(stream, "PSTATE\t: %c%c%c%c", state->conditionCodes.Negative ? 'N' : UNSET_CONDITION_CODE_CHAR,
|
|
state->conditionCodes.Zero ? 'Z' : UNSET_CONDITION_CODE_CHAR,
|
|
state->conditionCodes.Carry ? 'C' : UNSET_CONDITION_CODE_CHAR,
|
|
state->conditionCodes.Overflow ? 'V' : UNSET_CONDITION_CODE_CHAR);
|
|
}
|
|
|
|
// Returns the word starting at the provided address
|
|
word readWord(byte *memory, uint32_t address) {
|
|
word result = 0;
|
|
int bytesPerWord = WORD_BITS / BYTE_BITS - 1;
|
|
for (int i = 0; i <= bytesPerWord; i++)
|
|
result |= (word) memory[address + i] << (BYTE_BITS * i);
|
|
return result;
|
|
}
|
|
|
|
// Returns the double word starting at the provided address
|
|
dword readDoubleWord(byte *memory, uint32_t address) {
|
|
dword result = 0;
|
|
int bytesPerDword = DWORD_BITS / BYTE_BITS - 1;
|
|
for (int i = 0; i <= bytesPerDword; i++)
|
|
result |= (dword) memory[address + i] << (BYTE_BITS * i);
|
|
return result;
|
|
}
|
|
|
|
// Prints all non-zero memory locations into the provided stream
|
|
void printMemory(Machine *state, FILE *stream) {
|
|
fprintf(stream, "\nNon-zero memory:\n");
|
|
|
|
// print memory 4 byte aligned
|
|
for (int addr = 0; addr < MEMORY_SIZE; addr+= 4) {
|
|
word data = readWord(state->memory, addr);
|
|
if (data != 0) {
|
|
fprintf(stream, "0x%08x: %08x\n", addr, data);
|
|
}
|
|
}
|
|
}
|