Finally move showing registers to a function
All checks were successful
/ deploy (push) Successful in 1m19s
All checks were successful
/ deploy (push) Successful in 1m19s
Also added a cool frame
This commit is contained in:
parent
0e2d8ac37c
commit
0e9a116bad
3 changed files with 63 additions and 56 deletions
|
@ -19,5 +19,6 @@ typedef struct CPU_s CPU;
|
|||
|
||||
CPU create_cpu(AddressSpace *addressSpace);
|
||||
int cpu_cycle(CPU *cpu);
|
||||
void print_registers(CPU *cpu);
|
||||
|
||||
#endif
|
61
src/cpu.c
61
src/cpu.c
|
@ -1,5 +1,7 @@
|
|||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "address_space.h"
|
||||
#include "instruction_executor.h"
|
||||
|
@ -28,4 +30,63 @@ int cpu_cycle(CPU *cpu) {
|
|||
cpu->programCounter += 4;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void print_registers(CPU *cpu) {
|
||||
uint32_t *registers = cpu->registers;
|
||||
|
||||
// "%d: \033[1m0x%X\033[0m (%u)"
|
||||
|
||||
int columns = 8;
|
||||
int rows = 4;
|
||||
|
||||
int *colSize = malloc(columns * sizeof(int));
|
||||
int totalWidth = 0;
|
||||
|
||||
for (int coln=0; coln<columns; coln++) {
|
||||
int mlen = 0;
|
||||
for (int rown=0; rown<rows; rown++) {
|
||||
int i = rown * columns + coln;
|
||||
int len = snprintf(NULL, 0, "x%d: \033[1m0x%X\033[0m (%u)", i, registers[i], registers[i]);
|
||||
if (len > mlen) {
|
||||
mlen = len;
|
||||
}
|
||||
}
|
||||
colSize[coln] = mlen;
|
||||
totalWidth += mlen;
|
||||
}
|
||||
|
||||
printf("┌");
|
||||
for (int coln=0; coln<columns; coln++) {
|
||||
if (coln != 0) printf("┬");
|
||||
for (int i=0; i<colSize[coln] - 7; i++) {
|
||||
printf("─");
|
||||
}
|
||||
}
|
||||
printf("──┐\n");
|
||||
|
||||
int i = 0;
|
||||
for (int rown=0; rown<rows; rown++) {
|
||||
printf("│ ");
|
||||
for (int coln=0; coln<columns; coln++) {
|
||||
//printf("%d", colSize[coln]);
|
||||
char *entry = malloc(colSize[coln]);
|
||||
int len = snprintf(entry, colSize[coln]+1, "x%d: \033[1m0x%X\033[0m (%u)", i, registers[i], registers[i]);
|
||||
memset(entry + len, ' ', colSize[coln] - len);
|
||||
|
||||
printf("%s ", entry);
|
||||
|
||||
i++;
|
||||
}
|
||||
printf("│\n");
|
||||
}
|
||||
|
||||
printf("└");
|
||||
for (int coln=0; coln<columns; coln++) {
|
||||
for (int i=0; i<colSize[coln] - 6; i++) {
|
||||
printf("─");
|
||||
}
|
||||
}
|
||||
printf("─┘\n");
|
||||
// separators at the bottom look better but the font is aligned down so there's space at the top
|
||||
}
|
57
src/main.c
57
src/main.c
|
@ -20,7 +20,6 @@ int main(int argc, char *argv[]) {
|
|||
CPU cpu = create_cpu(addressSpace);
|
||||
cpu.registers[1] = addressSpace->romSize + addressSpace->ramSize; // make jumping to x1 end the program
|
||||
|
||||
|
||||
int lres = load_to_rom(argv[1], addressSpace);
|
||||
|
||||
if (lres == -1) {
|
||||
|
@ -48,61 +47,7 @@ int main(int argc, char *argv[]) {
|
|||
printf(" Program exit code: \033[1m%d\033[0m (x10)\n", cpu.registers[10]);
|
||||
printf(" Cycles: \033[1m%u\033[0m\n\n", cycles);
|
||||
|
||||
// TODO don't
|
||||
|
||||
int cols[8] = {0};
|
||||
int total = 0;
|
||||
|
||||
for (int col=0; col<8; col++) {
|
||||
int len;
|
||||
for (int row=0; row<4; row++) {
|
||||
int i = 8 * row + col;
|
||||
len = snprintf(NULL, 0, "%d: \033[1m0x%X\033[0m (%u)", i, cpu.registers[i], cpu.registers[i]);
|
||||
if (len > cols[col]) {
|
||||
cols[col] = len;
|
||||
}
|
||||
}
|
||||
total += len;
|
||||
}
|
||||
|
||||
printf("+");
|
||||
for (int i=0; i<8; i++) {
|
||||
for (int j=0; j<cols[i] - 6; j++) {
|
||||
printf("-");
|
||||
}
|
||||
printf("+");
|
||||
}
|
||||
|
||||
for (int i=0; i<32; i++) {
|
||||
if (i % 8 == 0) {
|
||||
printf("\n| ");
|
||||
}
|
||||
int row = i / 8;
|
||||
int col = i - row * 8;
|
||||
|
||||
int len = cols[col];
|
||||
char *entry = malloc(len);
|
||||
char *entryy = malloc(len);
|
||||
memset(entry, ' ', len);
|
||||
|
||||
int flen = snprintf(entryy, len+1, "%d: \033[1m0x%X\033[0m (%u)", i, cpu.registers[i], cpu.registers[i]);
|
||||
for (int j=0; j<flen; j++) {
|
||||
entry[j] = entryy[j];
|
||||
}
|
||||
|
||||
printf("%s | ", entry);
|
||||
}
|
||||
|
||||
printf("\n+");
|
||||
for (int i=0; i<8; i++) {
|
||||
for (int j=0; j<cols[i] - 6; j++) {
|
||||
printf("-");
|
||||
}
|
||||
printf("+");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
printf("The register dump might have some visual glitches, the data itself is ok\n"); // TODO fix
|
||||
print_registers(&cpu);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue