Added running example

This commit is contained in:
lcbcFoo 2017-11-08 10:58:43 -02:00
parent e6101c9527
commit 39c4d10155
7 changed files with 232 additions and 0 deletions

BIN
riscv/.main.c.swp Normal file

Binary file not shown.

36
riscv/Makefile Normal file
View file

@ -0,0 +1,36 @@
# Author: Lucas Castro
# Last modification: sep/2017
#
# Makefile for creating a raw binary RV32I program for ReonV
CC=gcc
AS=as
OBJCOPY=objcopy
CROSS=/home/foo/IC/riscv-leon/rv32i/rv32i-gnu/bin/riscv32-unknown-elf-
CFLAGS=-nostdlib
OBJCPFLAGS=-O binary -j .text
CPPROGRAM=
# Target .s
%.s : %.c
${CROSS}$(CC) -S $(CFLAGS) $(CPPFLAGS) $< -o $@
# Target .o
%.hex : %.s
${CROSS}$(AS) $< -o $@
# Target raw binary
%.bin : %.hex
${CROSS}$(OBJCOPY) $(OBJCPFLAGS) $< $@
# Inverts target for grmon load (it considers binary is big-endian)
%.inv : %.bin
./swap $< $@
cp-program :
cp $(CPPROGRAM) ./repo/designs/leon3-digilent-xc7z020/
clean:
rm -rf *.inv *.bin *.o *.log *.jou *.str

BIN
riscv/main.bin Normal file

Binary file not shown.

18
riscv/main.c Normal file
View file

@ -0,0 +1,18 @@
#include <stdio.h>
int mul();
void _start(){
int i = 1;
int count = 0;
do{
i = mul(i,4);
count++;
}while(i < 2500);
}
int mul(int i,int j){
if (i == 0)
return 0;
return mul(i - 1,j) + j;
}

76
riscv/main.s Normal file
View file

@ -0,0 +1,76 @@
# .file "teste.c"
# .option nopic
# .text
#.align 2
# .globl _start
# .type _start, @function
_start:
lui sp,0x41200
add sp,sp,-32
sw ra,28(sp)
sw s0,24(sp)
add s0,sp,32
li a5,1
sw a5,-20(s0)
sw zero,-24(s0)
.L2:
li a1,4
lw a0,-20(s0)
#call mul
jal ra,mul
sw a0,-20(s0)
lw a5,-24(s0)
add a5,a5,1
####################
mv x7,a5
####################
sw a5,-24(s0)
lw a4,-20(s0)
li a5,4096
add a5,a5,-1597
ble a4,a5,.L2
nop
lw ra,28(sp)
lw s0,24(sp)
add sp,sp,32
#############
# Move result to x6 and stop program
mv x6,a4
sbreak
#############
jr ra
# .size _start, .-_start
#.align 2
# .globl mul
# .type mul, @function
mul:
add sp,sp,-32
sw ra,28(sp)
sw s0,24(sp)
add s0,sp,32
sw a0,-20(s0)
sw a1,-24(s0)
lw a5,-20(s0)
bnez a5,.L4
li a5,0
j .L5
.L4:
lw a5,-20(s0)
add a5,a5,-1
lw a1,-24(s0)
mv a0,a5
#call mul
jal ra,mul
mv a4,a0
lw a5,-24(s0)
add a5,a4,a5
.L5:
mv a0,a5
lw ra,28(sp)
lw s0,24(sp)
add sp,sp,32
jr ra
.size mul, .-mul
.ident "GCC: (GNU) 7.1.1 20170509"

52
riscv/swapendian.c Normal file
View file

@ -0,0 +1,52 @@
/* Author: Lucas Castro
* Date: 10/09/2017
*
* Swaps the "endian" of a binary file (little to big or big to little)
*/
#include <stdio.h>
#include <stdint.h>
int main(int argc, char* argv[]){
uint32_t word;
uint32_t b0,b1,b2,b3;
uint32_t res;
unsigned char in_name[30];
unsigned char out_name[30];
FILE* input;
FILE* output;
if(argc != 3){
printf("Usage: ./main <input bin file> <output bin file>\n");
return 1;
}
input = fopen(argv[1], "rb");
output = fopen(argv[2], "wb");
if(input == NULL){
printf("Error opening input!\n");
return 1;
}
if(output == NULL){
fclose(input);
printf("Error opening output!\n");
return 1;
}
while(fread(&word, sizeof(uint32_t), 1, input)){
b0 = (word & 0x000000ff) << 24u;
b1 = (word & 0x0000ff00) << 8u;
b2 = (word & 0x00ff0000) >> 8u;
b3 = (word & 0xff000000) >> 24u;
res = b0 | b1 | b2 | b3;
fwrite(&res, sizeof(uint32_t), 1, output);
}
fclose(input);
fclose(output);
return 0;
}

50
riscv/to_text.c Normal file
View file

@ -0,0 +1,50 @@
/* Author: Lucas Castro
* Date: 10/09/2017
*
* Reads a binary file and converts its content to readable hexadecimal
*/
#include <stdio.h>
#include <stdint.h>
int main(int argc, char** argv){
char word;
char res[8];
uint8_t i = 0;
FILE* input;
FILE* output;
if(argc != 3){
printf("Usage: ./<program name> <input bin file> <output readable hex file>\n");
return 1;
}
input = fopen(argv[1], "rb");
output = fopen(argv[2], "w");
if(input == NULL){
printf("Error opening input!\n");
return 1;
}
if(output == NULL){
fclose(input);
printf("Error opening output!\n");
return 1;
}
while(fread(&word, sizeof(unsigned char), 1, input)){
sprintf(res, "%02X", (unsigned char) word & 0xff);
fwrite(&res, sizeof(unsigned char), 2, output);
i++;
if(i == 4){
fwrite("\n", sizeof(char), 1, output);
i = 0;
}
}
fclose(input);
fclose(output);
return 0;
}