Skip to content

Commit

Permalink
Implemented microcode reading.
Browse files Browse the repository at this point in the history
  • Loading branch information
linguini1 committed Jan 17, 2024
1 parent 87f921b commit 13871c8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
15 changes: 15 additions & 0 deletions emulator/src/components.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ word_t fetch_word(FILE *program, word_t addr) {
return (uint16_t)((word & 0x00FF) << 8) | (uint16_t)(word >> 8);
}

/**
* Reads the signals at the given address (state) of the decode ROM.
* @param decode_rom The file stream for the decode ROM/microcode.
* @param addr The address of the signals to read.
* @return The signals stored at the given address.
*/
signals_t fetch_signals(FILE *decode_rom, uint8_t addr) {

fseek(decode_rom, addr * 2, SEEK_SET);

signals_t signals;
fread(&signals, sizeof(signals), 1, decode_rom);
return signals; // Don't care that this is little-endian, because signals are bit values
}

static word_t rotr(word_t a, word_t n) { return (a >> n) | (a << (sizeof(word_t) * 8 - n)); }
static word_t rotl(word_t a, word_t n) { return (a << n) | (a >> (sizeof(word_t) * 8 - n)); }

Expand Down
3 changes: 3 additions & 0 deletions emulator/src/components.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ typedef enum {

/** Defines a single memory word. */
typedef uint16_t word_t;
/** Defines a set of internal signals. */
typedef uint64_t signals_t;

word_t fetch_word(FILE *program, word_t addr);
signals_t fetch_signals(FILE *decode_rom, uint8_t addr);
word_t alu(ALUOperation op, word_t a, word_t b, uint8_t *flags);

#endif // _COMPONENTS_H_
27 changes: 20 additions & 7 deletions emulator/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,46 @@
#include <stdio.h>
#include <stdlib.h>

static char *filename = NULL;
static uint16_t pc = 0;

int main(int argc, char **argv) {

// Get program to run
if (argc != 2) {
fprintf(stderr, "You must provide an input program file.\n");
if (argc != 3) {
fprintf(stderr, "You must provide microcode for the processor and an input program file.\n");
return EXIT_FAILURE;
}

// Open microcode
FILE *microcode = fopen(argv[1], "rb");
if (microcode == NULL) {
fprintf(stderr, "Could not open microcode file '%s'\n", argv[1]);
return EXIT_FAILURE;
}

// Open program
filename = argv[1];
FILE *program = fopen(filename, "rb");
FILE *program = fopen(argv[2], "rb");
if (program == NULL) {
fprintf(stderr, "Could not open file '%s'.\n", filename);
fprintf(stderr, "Could not open program file '%s'.\n", argv[2]);
return EXIT_FAILURE;
}

// Display program
while (!feof(program) && !ferror(program)) {
uint16_t word = fetch_word(program, pc);
printf("original: %04x\n", word);
printf("%04x\n", word);
pc++;
}

uint8_t addr = 0;
while (!feof(microcode) && !ferror(microcode)) {
uint64_t signals = fetch_signals(microcode, addr);
printf("%016llx\n", signals);
addr++;
}

// Close stream when done
fclose(microcode);
fclose(program);

return EXIT_SUCCESS;
Expand Down

0 comments on commit 13871c8

Please sign in to comment.