Update read word to be utility func, w/ T

This commit is contained in:
sBubshait 2024-06-02 21:45:57 +01:00
parent 6bc15b7faf
commit 78d1f5588f
2 changed files with 5 additions and 4 deletions

View File

@ -1,4 +1,5 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#include "print.h" #include "print.h"
#include "emulator.h" #include "emulator.h"
@ -25,12 +26,11 @@ void printRegisters(Machine *state, FILE *stream) {
} }
// Returns the word starting at the provided address // Returns the word starting at the provided address
// Converts 4 bytes into one word word readWord(byte *memory, uint32_t address) {
word readWord(Machine *state, word address) {
word result = 0; word result = 0;
int bytesPerWord = WORD_BITS / BYTE_BITS - 1; int bytesPerWord = WORD_BITS / BYTE_BITS - 1;
for (int i = 0; i <= bytesPerWord; i++) for (int i = 0; i <= bytesPerWord; i++)
result |= (word) state->memory[address + i] << (BYTE_BITS * (bytesPerWord - i)); result |= (word) memory[address + i] << (BYTE_BITS * (bytesPerWord - i));
return result; return result;
} }
@ -40,7 +40,7 @@ void printMemory(Machine *state, FILE *stream) {
// print memory 4 byte aligned // print memory 4 byte aligned
for (int addr = 0; addr < MEMORY_SIZE; addr+= 4) { for (int addr = 0; addr < MEMORY_SIZE; addr+= 4) {
word data = readWord(state, addr); word data = readWord(state->memory, addr);
if (data != 0) { if (data != 0) {
fprintf(stream, "0x%08x: 0x%08x\n", addr, data); fprintf(stream, "0x%08x: 0x%08x\n", addr, data);
} }

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include "emulator.h" #include "emulator.h"
word readWord(byte *memory, uint32_t address);
void printState(Machine *state, FILE *stream); void printState(Machine *state, FILE *stream);
void printRegisters(Machine *state, FILE *stream); void printRegisters(Machine *state, FILE *stream);
void printMemory(Machine *state, FILE *stream); void printMemory(Machine *state, FILE *stream);