From f6b5f0c394cec00e96636290e6a47a1843708330 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Tue, 15 Oct 2024 13:30:45 +0200 Subject: [PATCH] Update some --- include/address_space.h | 4 +++- include/cpu.h | 3 ++- include/instruction_executor.h | 3 +++ src/address_space.c | 21 +++++++-------------- src/cpu.c | 14 ++++++++++++++ src/instruction_executor.c | 9 +++++++++ src/main.c | 2 +- src/program_loader.c | 2 +- 8 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 include/instruction_executor.h create mode 100644 src/instruction_executor.c diff --git a/include/address_space.h b/include/address_space.h index b69fb66..35ccceb 100644 --- a/include/address_space.h +++ b/include/address_space.h @@ -9,4 +9,6 @@ struct AddressSpace_s { typedef struct AddressSpace_s AddressSpace; -AddressSpace create_address_space(int romSize, int ramSize); \ No newline at end of file +AddressSpace create_address_space(int romSize, int ramSize); +int read_address_space(const AddressSpace *addressSpace, uint32_t address, const int n, void *dest); +int write_address_space(const AddressSpace *addressSpace, uint32_t address, const int n, void *src); \ No newline at end of file diff --git a/include/cpu.h b/include/cpu.h index 793c5e9..37aa7a0 100644 --- a/include/cpu.h +++ b/include/cpu.h @@ -9,4 +9,5 @@ struct CPU_s { typedef struct CPU_s CPU; -CPU create_cpu(AddressSpace *addressSpace); \ No newline at end of file +CPU create_cpu(AddressSpace *addressSpace); +int cycle(CPU *cpu); \ No newline at end of file diff --git a/include/instruction_executor.h b/include/instruction_executor.h new file mode 100644 index 0000000..16d39d4 --- /dev/null +++ b/include/instruction_executor.h @@ -0,0 +1,3 @@ +#include "cpu.h" + +int execute_instruction_on_cpu(CPU *cpu, uint32_t instruction); \ No newline at end of file diff --git a/src/address_space.c b/src/address_space.c index 601508a..f53e760 100644 --- a/src/address_space.c +++ b/src/address_space.c @@ -1,5 +1,6 @@ #include #include +#include struct AddressSpace_s { // A pointer to a ROM array. The array can vary in length. @@ -25,7 +26,7 @@ AddressSpace create_address_space(int romSize, int ramSize) { return addressSpace; } -int read_address_space(const AddressSpace *addressSpace, uint32_t address, const int n, uint8_t *dest) { +int read_address_space(const AddressSpace *addressSpace, uint32_t address, const int n, void *dest) { uint32_t romSize = addressSpace->romSize; uint32_t ramSize = addressSpace->ramSize; @@ -34,9 +35,7 @@ int read_address_space(const AddressSpace *addressSpace, uint32_t address, const fprintf(stderr, "Reading %d bytes from %d will exceed ROM address space of %d\n", n, address, romSize); // TODO maybe move to perror return 1; } else { - for (int i=0; irom[address + i]; - } + memcpy(dest, addressSpace->rom + address, n); } } else if (address < romSize + ramSize) { if (address + n < romSize + ramSize) { @@ -44,9 +43,7 @@ int read_address_space(const AddressSpace *addressSpace, uint32_t address, const return 1; } else { address -= romSize; - for (int i=0; iram[address + i]; - } + memcpy(dest, addressSpace->ram + address, n); } } else { @@ -56,7 +53,7 @@ int read_address_space(const AddressSpace *addressSpace, uint32_t address, const return 0; } -int write_address_space(const AddressSpace *addressSpace, uint32_t address, const int n, uint8_t *src) { +int write_address_space(const AddressSpace *addressSpace, uint32_t address, const int n, void *src) { uint32_t romSize = addressSpace->romSize; uint32_t ramSize = addressSpace->ramSize; @@ -65,9 +62,7 @@ int write_address_space(const AddressSpace *addressSpace, uint32_t address, cons fprintf(stderr, "Writing %d bytes to %d will exceed ROM address space of %d\n", n, address, romSize); return 1; } else { - for (int i=0; irom[address + i] = src[i]; - } + memcpy(addressSpace->rom + address, src, n); } } else if (address < romSize + ramSize) { if (address + n < romSize + ramSize) { @@ -75,9 +70,7 @@ int write_address_space(const AddressSpace *addressSpace, uint32_t address, cons return 1; } else { address -= romSize; - for (int i=0; iram[address + i] = src[i]; - } + memcpy(addressSpace->ram + address, src, n); } } else { // TODO IO diff --git a/src/cpu.c b/src/cpu.c index 58d32a4..cf84db2 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -1,5 +1,6 @@ #include #include "address_space.h" +#include "instruction_executor.h" struct CPU_s { // [32] of 32bit (uint32_t) registers @@ -23,4 +24,17 @@ CPU create_cpu(AddressSpace *addressSpace) { cpu.addressSpace = addressSpace; return cpu; +} + +int cycle(CPU *cpu) { + uint32_t instruction; + if (read_address_space(cpu->addressSpace, cpu->programCounter, 4, &instruction) != 0) { + return -1; + } + + execute_instruction_on_cpu(cpu, instruction); + + cpu->programCounter += 4; + + return 0; } \ No newline at end of file diff --git a/src/instruction_executor.c b/src/instruction_executor.c new file mode 100644 index 0000000..cfbb514 --- /dev/null +++ b/src/instruction_executor.c @@ -0,0 +1,9 @@ +#include "cpu.h" + +int execute_instruction_on_cpu(CPU *cpu, uint32_t instruction) { + AddressSpace *addressSpace = cpu->addressSpace; + + // TODO + + return 0; +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index c5a0a9a..9127476 100644 --- a/src/main.c +++ b/src/main.c @@ -4,7 +4,7 @@ #include "cpu.h" int main(int argc, char *argv[]) { - if (argc == 0) { + if (argc == 1) { fprintf(stderr, "Usage: criscv \n"); return 1; } diff --git a/src/program_loader.c b/src/program_loader.c index 827c75d..3b16884 100644 --- a/src/program_loader.c +++ b/src/program_loader.c @@ -5,7 +5,7 @@ int load_to_rom(const char filename[], AddressSpace *addressSpace) { FILE *file = fopen(filename, "rb"); if (file == NULL) { - perror("Error opening file\n"); + perror("Error opening file"); return 1; }