56 lines
No EOL
1.5 KiB
C
56 lines
No EOL
1.5 KiB
C
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "program_loader.h"
|
|
#include "elf_program_loader.h"
|
|
#include "cpu.h"
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc == 1) {
|
|
fprintf(stderr, "Usage: criscv <binary program>\n");
|
|
return 1;
|
|
}
|
|
|
|
AddressSpace *addressSpace = create_address_space(2048, 512);
|
|
printf("Address space: %dB ROM, %dB RAM\n", addressSpace->romSize, addressSpace->ramSize);
|
|
|
|
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) {
|
|
lres = load_elf_to_cpu_and_rom(argv[1], &cpu);
|
|
}
|
|
|
|
if (lres != 0) {
|
|
fprintf(stderr, "Error loading program\n");
|
|
return 1;
|
|
}
|
|
|
|
addressSpace->romLocked = true;
|
|
|
|
|
|
printf("\n----- Start of program (0x%X) -----", cpu.programCounter);
|
|
|
|
int code;
|
|
uint32_t cycles = 0;
|
|
while ((code = cpu_cycle(&cpu)) == 0) {
|
|
cycles++;
|
|
//sleep(1);
|
|
}
|
|
|
|
printf("\n\n----- End of program (0x%X) -----\n", cpu.programCounter);
|
|
|
|
printf("\n Emulator exit code: \033[1m%d\033[0m\n", code);
|
|
printf(" Program exit code: \033[1m%d\033[0m (x10)\n", cpu.registers[10]);
|
|
printf(" Cycles: \033[1m%u\033[0m\n\n", cycles);
|
|
|
|
cpu.registers[0] = 0; // if x0 is changed in the last instruction (it usually is) it will be displayed what if was set to
|
|
print_registers(&cpu);
|
|
|
|
return 0;
|
|
} |