First real program running!

This commit is contained in:
2024-05-12 01:26:32 +02:00
parent 7642349be9
commit b9dd016064
2 changed files with 67 additions and 16 deletions

View File

@ -216,6 +216,8 @@ struct Hart
{
uint32_t pc;
uint32_t regs[32];
char* mem;
uint32_t mem_size;
};
void execute_op_imm(struct Hart* hart, uint32_t instruction)
@ -334,24 +336,61 @@ void execute_branch(struct Hart* hart, uint32_t instruction)
}
}
inline uint32_t load_size(struct Hart* hart, uint32_t address, uint32_t size)
{
if ((address & 0x80000000) == 0)
{
assert(address + size < hart->mem_size);
uint32_t value = 0;
memcpy(&value, hart->mem + address, size);
return value;
}
return 0;
}
uint32_t load_byte(struct Hart* hart, uint32_t address)
{
return 0;
return load_size(hart, address, 1);
}
uint32_t load_half(struct Hart* hart, uint32_t address)
{
return 0;
return load_size(hart, address, 2);
}
uint32_t load_word(struct Hart* hart, uint32_t address)
{
return 0;
return load_size(hart, address, 4);
}
void store_byte(struct Hart* hart, uint32_t address, uint8_t value) {}
void store_half(struct Hart* hart, uint32_t address, uint16_t value) {}
void store_word(struct Hart* hart, uint32_t address, uint32_t value) {}
inline void store_size(struct Hart* hart, uint32_t address, uint32_t value, uint32_t size)
{
if ((address & 0x80000000) == 0)
{
assert(address + size < hart->mem_size);
memcpy(hart->mem + address, &value, size);
}
else if (address == 0x80000000)
{
fwrite(&value, 1, size, stdout);
}
}
void store_byte(struct Hart* hart, uint32_t address, uint8_t value)
{
store_size(hart, address, value, 1);
}
void store_half(struct Hart* hart, uint32_t address, uint16_t value)
{
store_size(hart, address, value, 2);
}
void store_word(struct Hart* hart, uint32_t address, uint32_t value)
{
store_size(hart, address, value, 4);
}
void execute_op_load(struct Hart* hart, uint32_t instruction)
{
@ -786,14 +825,13 @@ int main(int argc, char* argv[])
struct Hart hart = {0};
hart.pc = start_address;
hart.mem = mem;
hart.mem_size = mem_size;
while (true)
{
uint32_t instruction = *(uint32_t*)(mem + hart.pc);
uint32_t instruction = load_word(&hart, hart.pc);
execute(&hart, instruction);
printf("pc=%x x1=%d x2=%d x3=%d x4=%d\n",
hart.pc, hart.regs[1], hart.regs[2], hart.regs[3], hart.regs[4]);
fgetc(stdout);
}
return EXIT_SUCCESS;