JAL fix and other stuff
All checks were successful
/ deploy (push) Successful in 1m17s

This commit is contained in:
Minecon724 2024-10-22 15:56:53 +02:00
parent 16975eced0
commit 52882cb6a6
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
5 changed files with 67201 additions and 29 deletions

BIN
programs/rodata.elf Executable file

Binary file not shown.

53
programs/src/rodata.c Normal file
View file

@ -0,0 +1,53 @@
// Read-only data section
__attribute__((section(".rodata")))
static const unsigned int error_codes[] = {
0x00000001, // Error 1
0x00000002, // Error 2
0x00000004, // Error 3
0x00000008 // Error 4
};
// Another read-only table
__attribute__((section(".rodata")))
static const unsigned char status_codes[] = {
0x10, // Status OK
0x20, // Status Warning
0x30, // Status Error
0x40 // Status Critical
};
// Function to get error code
unsigned int get_error_code(unsigned int index) {
if (index < 4) {
return error_codes[index];
}
return 0;
}
// Function to get status
unsigned char get_status(unsigned int index) {
if (index < 4) {
return status_codes[index];
}
return 0;
}
// Function that performs operation using rodata
unsigned int check_status_and_error(unsigned int status_idx,
unsigned int error_idx) {
unsigned char status = get_status(status_idx);
unsigned int error = get_error_code(error_idx);
// Combine status and error
return (status << 24) | error;
}
int main(void) {
// Store results in variables, no printf needed
unsigned int result1 = get_error_code(0);
unsigned char result2 = get_status(1);
unsigned int combined = check_status_and_error(2, 3);
// Return final result
return combined;
}

View file

@ -54,14 +54,14 @@ static inline int process_elf(Elf *elf, CPU *cpu) {
char *sectionName = elf_strptr(elf, shdrstrndx, shdr.sh_name); char *sectionName = elf_strptr(elf, shdrstrndx, shdr.sh_name);
Elf_Data *data = elf_getdata(scn, NULL);
if (data == NULL || data->d_size == 0) {
fprintf(stderr, "elf_getdata() failed: %s\n", elf_errmsg(-1));
continue;
}
if (shdr.sh_type == SHT_RISCV_ATTRIBUTES) { if (shdr.sh_type == SHT_RISCV_ATTRIBUTES) {
Elf_Data *data = elf_getdata(scn, NULL);
if (data == NULL || data->d_size == 0) {
fprintf(stderr, "elf_getdata() failed: %s\n", elf_errmsg(-1));
continue;
}
// TODO this is highly fragile // TODO this is highly fragile
// https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc // https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc
char *content = (char *)data->d_buf; char *content = (char *)data->d_buf;
@ -69,30 +69,14 @@ static inline int process_elf(Elf *elf, CPU *cpu) {
if (strcmp(content, "rv32i2p1") == 0) { if (strcmp(content, "rv32i2p1") == 0) {
warnedArch = 1; warnedArch = 1;
} }
break;
} else if (strcmp(sectionName, ".text") == 0) { } else if (strcmp(sectionName, ".text") == 0) {
Elf_Data *data = elf_getdata(scn, NULL);
if (data == NULL || data->d_size == 0) {
fprintf(stderr, "elf_getdata() failed: %s\n", elf_errmsg(-1));
continue;
}
text = (uint8_t *)data->d_buf; text = (uint8_t *)data->d_buf;
textSize = data->d_size; textSize = data->d_size;
} else if (strcmp(sectionName, ".rodata") == 0) { } else if (strcmp(sectionName, ".rodata") == 0) {
Elf_Data *data = elf_getdata(scn, NULL);
if (data == NULL || data->d_size == 0) {
fprintf(stderr, "elf_getdata() failed: %s\n", elf_errmsg(-1));
continue;
}
rodata = (uint8_t *)data->d_buf; rodata = (uint8_t *)data->d_buf;
rodataSize = data->d_size; rodataSize = data->d_size;
} else { } else {
printf("Unrecognized section: %s\n", sectionName); printf("Unrecognized section: %s (%ub)\n", sectionName, (uint32_t)data->d_size);
} }
} }

View file

@ -224,13 +224,21 @@ int execute_instruction_on_cpu(CPU *cpu, uint32_t instruction) { // TODO conside
break; break;
} }
case 0b1101111: { // JAL for unconditional jump (J type) case 0b1101111: { // JAL for unconditional jump (J type)
int32_t imm = ((int32_t)instruction >> 31) << 20; // Extract imm[20] and sign-extend int32_t imm = (int32_t)instruction >> 31;
imm |= (instruction >> 21) & 0x3FF; // Extract imm[10:1] imm <<= 8;
imm |= (instruction >> 20) & 0x1; // Extract imm[11] imm |= instruction >> 12 & 0xFF;
imm |= (instruction >> 12) & 0xFF; // Extract imm[19:12] imm <<= 1;
imm <<= 1; // Left-shift by 1 to account for the implicit 0 imm |= instruction >> 20 & 0x1;
imm <<= 10;
imm |= instruction >> 21 & 0x3FF;
imm <<= 1;
registers[rd] = cpu->programCounter; // program counter is always incremented after executing instruction /*int32_t imm = ((int32_t)instruction >> 31) << 20; // Extract imm[20] and sign-extend
imm |= (instruction >> 21) & 0x3FF << 1; // Extract imm[10:1]
imm |= (instruction >> 20) & 0x1 << 11; // Extract imm[11]
imm |= (instruction >> 12) & 0xFF << 12; // Extract imm[19:12]*/
registers[rd] = cpu->programCounter + 4;
cpu->programCounter += imm; cpu->programCounter += imm;
printf("JAL: Jumped to %u + %d = 0x%X (inst %d)", registers[rd], imm, cpu->programCounter, cpu->programCounter / 4); printf("JAL: Jumped to %u + %d = 0x%X (inst %d)", registers[rd], imm, cpu->programCounter, cpu->programCounter / 4);

67127
unpriv-isa-asciidoc.html Normal file

File diff suppressed because one or more lines are too long