Update some

This commit is contained in:
Minecon724 2024-10-15 13:30:45 +02:00
parent c8a56a78ec
commit f6b5f0c394
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
8 changed files with 40 additions and 18 deletions

View file

@ -10,3 +10,5 @@ struct AddressSpace_s {
typedef struct AddressSpace_s AddressSpace;
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);

View file

@ -10,3 +10,4 @@ struct CPU_s {
typedef struct CPU_s CPU;
CPU create_cpu(AddressSpace *addressSpace);
int cycle(CPU *cpu);

View file

@ -0,0 +1,3 @@
#include "cpu.h"
int execute_instruction_on_cpu(CPU *cpu, uint32_t instruction);

View file

@ -1,5 +1,6 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
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; i<n; i++) {
dest[i] = addressSpace->rom[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; i<n; i++) {
dest[i] = addressSpace->ram[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; i<n; i++) {
addressSpace->rom[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; i<n; i++) {
addressSpace->ram[address + i] = src[i];
}
memcpy(addressSpace->ram + address, src, n);
}
} else {
// TODO IO

View file

@ -1,5 +1,6 @@
#include <stdint.h>
#include "address_space.h"
#include "instruction_executor.h"
struct CPU_s {
// [32] of 32bit (uint32_t) registers
@ -24,3 +25,16 @@ CPU create_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;
}

View file

@ -0,0 +1,9 @@
#include "cpu.h"
int execute_instruction_on_cpu(CPU *cpu, uint32_t instruction) {
AddressSpace *addressSpace = cpu->addressSpace;
// TODO
return 0;
}

View file

@ -4,7 +4,7 @@
#include "cpu.h"
int main(int argc, char *argv[]) {
if (argc == 0) {
if (argc == 1) {
fprintf(stderr, "Usage: criscv <binary program>\n");
return 1;
}

View file

@ -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;
}