Update some
This commit is contained in:
parent
c8a56a78ec
commit
f6b5f0c394
8 changed files with 40 additions and 18 deletions
|
@ -9,4 +9,6 @@ struct AddressSpace_s {
|
||||||
|
|
||||||
typedef struct AddressSpace_s AddressSpace;
|
typedef struct AddressSpace_s AddressSpace;
|
||||||
|
|
||||||
AddressSpace create_address_space(int romSize, int ramSize);
|
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);
|
|
@ -9,4 +9,5 @@ struct CPU_s {
|
||||||
|
|
||||||
typedef struct CPU_s CPU;
|
typedef struct CPU_s CPU;
|
||||||
|
|
||||||
CPU create_cpu(AddressSpace *addressSpace);
|
CPU create_cpu(AddressSpace *addressSpace);
|
||||||
|
int cycle(CPU *cpu);
|
3
include/instruction_executor.h
Normal file
3
include/instruction_executor.h
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
#include "cpu.h"
|
||||||
|
|
||||||
|
int execute_instruction_on_cpu(CPU *cpu, uint32_t instruction);
|
|
@ -1,5 +1,6 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
struct AddressSpace_s {
|
struct AddressSpace_s {
|
||||||
// A pointer to a ROM array. The array can vary in length.
|
// 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;
|
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 romSize = addressSpace->romSize;
|
||||||
uint32_t ramSize = addressSpace->ramSize;
|
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
|
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;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
for (int i=0; i<n; i++) {
|
memcpy(dest, addressSpace->rom + address, n);
|
||||||
dest[i] = addressSpace->rom[address + i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (address < romSize + ramSize) {
|
} else if (address < romSize + ramSize) {
|
||||||
if (address + n < romSize + ramSize) {
|
if (address + n < romSize + ramSize) {
|
||||||
|
@ -44,9 +43,7 @@ int read_address_space(const AddressSpace *addressSpace, uint32_t address, const
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
address -= romSize;
|
address -= romSize;
|
||||||
for (int i=0; i<n; i++) {
|
memcpy(dest, addressSpace->ram + address, n);
|
||||||
dest[i] = addressSpace->ram[address + i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -56,7 +53,7 @@ int read_address_space(const AddressSpace *addressSpace, uint32_t address, const
|
||||||
return 0;
|
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 romSize = addressSpace->romSize;
|
||||||
uint32_t ramSize = addressSpace->ramSize;
|
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);
|
fprintf(stderr, "Writing %d bytes to %d will exceed ROM address space of %d\n", n, address, romSize);
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
for (int i=0; i<n; i++) {
|
memcpy(addressSpace->rom + address, src, n);
|
||||||
addressSpace->rom[address + i] = src[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (address < romSize + ramSize) {
|
} else if (address < romSize + ramSize) {
|
||||||
if (address + n < romSize + ramSize) {
|
if (address + n < romSize + ramSize) {
|
||||||
|
@ -75,9 +70,7 @@ int write_address_space(const AddressSpace *addressSpace, uint32_t address, cons
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
address -= romSize;
|
address -= romSize;
|
||||||
for (int i=0; i<n; i++) {
|
memcpy(addressSpace->ram + address, src, n);
|
||||||
addressSpace->ram[address + i] = src[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO IO
|
// TODO IO
|
||||||
|
|
14
src/cpu.c
14
src/cpu.c
|
@ -1,5 +1,6 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "address_space.h"
|
#include "address_space.h"
|
||||||
|
#include "instruction_executor.h"
|
||||||
|
|
||||||
struct CPU_s {
|
struct CPU_s {
|
||||||
// [32] of 32bit (uint32_t) registers
|
// [32] of 32bit (uint32_t) registers
|
||||||
|
@ -23,4 +24,17 @@ CPU create_cpu(AddressSpace *addressSpace) {
|
||||||
cpu.addressSpace = addressSpace;
|
cpu.addressSpace = addressSpace;
|
||||||
|
|
||||||
return cpu;
|
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;
|
||||||
}
|
}
|
9
src/instruction_executor.c
Normal file
9
src/instruction_executor.c
Normal 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;
|
||||||
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
#include "cpu.h"
|
#include "cpu.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc == 0) {
|
if (argc == 1) {
|
||||||
fprintf(stderr, "Usage: criscv <binary program>\n");
|
fprintf(stderr, "Usage: criscv <binary program>\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ int load_to_rom(const char filename[], AddressSpace *addressSpace) {
|
||||||
FILE *file = fopen(filename, "rb");
|
FILE *file = fopen(filename, "rb");
|
||||||
|
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
perror("Error opening file\n");
|
perror("Error opening file");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue