1 #*********************************************************************************************** 2 # File : Makefile 3 # Author : Lyu Yang 4 # Date : 5 # Description : Makefile for RISC-V 6 #*********************************************************************************************** 7 8 TARGET = main 9 10 CROSS_COMPILE = riscv64-unknown-elf- 11 12 INCLUDE_DIRS = -I ./ 13 LIBRARY_DIRS = -L ./ 14 15 CC = $(CROSS_COMPILE)gcc 16 CFLAGS = $(INCLUDE_DIRS) -c -mabi=ilp32 -march=rv32imc -Wall -O2 -std=c99 -gdwarf-2 -freorder-blocks-algorithm=simple -fno-schedule-insns -nostdinc -fno-aggressive-loop-optimizations -fno-builtin -Wpointer-arith -Wstrict-prototypes -Wno-write-strings -fno-exceptions -fno-short-enums -Wundef -falign-functions=2 -fdata-sections -ffunction-sections -fno-common 17 18 CXX = $(CROSS_COMPILE)g++ 19 CXXFLAGS = $(INCLUDE_DIRS) -c -mabi=ilp32 -march=rv32imc -Wall -O2 -std=c99 -gdwarf-2 -freorder-blocks-algorithm=simple -fno-schedule-insns -nostdinc -fno-aggressive-loop-optimizations -fno-builtin -Wpointer-arith -Wstrict-prototypes -Wno-write-strings -fno-exceptions -fno-short-enums -Wundef -falign-functions=2 -fdata-sections -ffunction-sections -fno-common 20 21 AS = $(CROSS_COMPILE)gcc 22 ASFLAGS = -c -mabi=ilp32 -march=rv32imc -x assembler-with-cpp -Wall -Werror -O2 -gdwarf-2 -nostdinc -fstack-protector -fno-common -fdata-sections -ffunction-sections 23 24 LD = $(CROSS_COMPILE)gcc 25 LDFLAGS = $(LIBRARY_DIRS) -mabi=ilp32 -march=rv32im -Triscv.lds -nostartfiles -nodefaultlibs -nostdlib 26 27 OBJCP = $(CROSS_COMPILE)objcopy 28 OBJCPFLAGS = -O binary -j .text -j .data -j .sdata 29 30 AR = $(CROSS_COMPILE)ar 31 ARFLAGS = cr 32 33 DUMP = $(CROSS_COMPILE)objdump 34 DUMPFLAG = --disassemble --syms --all-header 35 36 SIZE = $(CROSS_COMPILE)size 37 38 # Static library files 39 OBJS_LIB += 40 41 # User should list all object files 42 OBJS += $(patsubst %.S, %.o, $(wildcard *.S)) 43 OBJS += $(patsubst %.c, %.o, $(wildcard *.c)) 44 45 # Make 46 .PHONY: all clean debug 47 all: $(TARGET).elf $(TARGET).bin $(TARGET).asm $(TARGET).mif 48 $(SIZE) $(TARGET).elf 49 50 %.o: %.S 51 $(AS) $(ASFLAGS) $^ -o $@ 52 53 %.o: %.c 54 $(CC) $(CFLAGS) $^ -o $@ 55 56 $(TARGET).elf: $(OBJS) 57 $(LD) $(LDFLAGS) $(OBJS_LIB) -o $@ $^ 58 59 $(TARGET).asm: $(TARGET).elf 60 $(DUMP) $(DUMPFLAG) $(TARGET).elf > $(TARGET).asm 61 62 $(TARGET).bin: $(TARGET).elf 63 $(OBJCP) $(OBJCPFLAGS) $< $@ 64 65 $(TARGET).mif: $(TARGET).bin 66 ./bin2fpga/bin2fpga 4096 $< 67 68 clean: 69 rm -rf *.o *.asm *.elf *.bin *.mif *.map *.coe data.txt 70 71 debug: 72 $(CROSS_COMPILE)gdb $(TARGET).elf -x gdbinit.txt
1 OUTPUT_ARCH("riscv") 2 ENTRY(_start) 3 4 MEMORY 5 { 6 RAM (rwx) : ORIGIN = 0x0, LENGTH = 64K 7 } 8 9 STACK_SIZE = 2k; 10 HEAP_SIZE = 2k; 11 12 SECTIONS 13 { 14 .text : ALIGN(4) 15 { 16 __start_addr = .; 17 KEEP(*(.text.entry)) 18 . = ALIGN(4); 19 *(.text*) 20 . = ALIGN(4); 21 __rodata_start = .; 22 *(.rodata*) 23 . = ALIGN(4); 24 __rodata_end = .; 25 __text_end = .; 26 } > RAM 27 28 /* data section */ 29 .data : 30 { 31 __data_load = LOADADDR(.data); 32 __data_start = .; 33 *(.data*) 34 . = ALIGN(4); 35 __data_end = .; 36 } > RAM 37 38 /* short/global data section */ 39 .sdata : 40 { 41 __sdata_load = LOADADDR(.sdata); 42 __sdata_start = .; 43 _gp = . + 0x100; 44 *(.srodata*) 45 *(.sdata*) 46 __sdata_end = .; 47 } > RAM 48 49 /* sbss section */ 50 .sbss : ALIGN(4) 51 { 52 __sbss_start = .; 53 *(.sbss*) 54 *(.scommon) 55 . = ALIGN(4); 56 __sbss_end = .; 57 } > RAM 58 59 /* bss section */ 60 .bss : ALIGN(4) 61 { 62 __bss_begin = .; 63 *(.bss*) 64 *(COMMON) 65 . = ALIGN(4); 66 __bss_end = .; 67 } > RAM 68 __bss_size = __bss_end - __bss_begin; 69 _end = .; 70 71 .stack : ALIGN(4) 72 { 73 __stack_bottom = .; 74 . += STACK_SIZE; 75 __stack_top = .; 76 } > RAM 77 78 .heap : ALIGN(4) 79 { 80 __heap_begin = .; 81 . += HEAP_SIZE; 82 __heap_end = .; 83 } > RAM 84 }
.extern __stack_top .macro push_reg addi sp, sp, -32*4 sw x1, 0 * 4(sp) sw x2, 1 * 4(sp) sw x3, 2 * 4(sp) sw x4, 3 * 4(sp) sw x5, 4 * 4(sp) sw x6, 5 * 4(sp) sw x7, 6 * 4(sp) sw x8, 7 * 4(sp) sw x9, 8 * 4(sp) sw x10, 9 * 4(sp) sw x11, 10 * 4(sp) sw x12, 11 * 4(sp) sw x13, 12 * 4(sp) sw x14, 13 * 4(sp) sw x15, 14 * 4(sp) sw x16, 15 * 4(sp) sw x17, 16 * 4(sp) sw x18, 17 * 4(sp) sw x19, 18 * 4(sp) sw x20, 19 * 4(sp) sw x21, 20 * 4(sp) sw x22, 21 * 4(sp) sw x23, 22 * 4(sp) sw x24, 23 * 4(sp) sw x25, 24 * 4(sp) sw x26, 25 * 4(sp) sw x27, 26 * 4(sp) sw x28, 27 * 4(sp) sw x29, 28 * 4(sp) sw x30, 29 * 4(sp) sw x31, 30 * 4(sp) csrr t0, mepc sw t0, 31 * 4(sp) .endm .macro pop_reg lw x1, 0 * 4(sp) lw x5, 4 * 4(sp) lw x6, 5 * 4(sp) lw x7, 6 * 4(sp) lw x8, 7 * 4(sp) lw x9, 8 * 4(sp) lw x10, 9 * 4(sp) lw x11, 10 * 4(sp) lw x12, 11 * 4(sp) lw x13, 12 * 4(sp) lw x14, 13 * 4(sp) lw x15, 14 * 4(sp) lw x16, 15 * 4(sp) lw x17, 16 * 4(sp) lw x18, 17 * 4(sp) lw x19, 18 * 4(sp) lw x20, 19 * 4(sp) lw x21, 20 * 4(sp) lw x22, 21 * 4(sp) lw x23, 22 * 4(sp) lw x24, 23 * 4(sp) lw x25, 24 * 4(sp) lw x26, 25 * 4(sp) lw x27, 26 * 4(sp) lw x28, 27 * 4(sp) lw x29, 28 * 4(sp) lw x30, 29 * 4(sp) lw x31, 30 * 4(sp) addi sp, sp, 32*4 .endm .section .text.entry .option norvc .global _start _start: tail handle_reset trap_vector: j RVDefaultHandler j RVDefaultHandler // 1 j RVDefaultHandler // 2 j RVDefaultHandler // 3 j RVDefaultHandler // 4 j RVDefaultHandler // 5 j RVDefaultHandler // 6 j RVDefaultHandler // 7 j RVDefaultHandler // 8 j RVDefaultHandler // 9 j RVDefaultHandler // 10 j RVDefaultHandler // 11 j RVDefaultHandler // 12 j RVDefaultHandler // 13 j RVDefaultHandler // 14 j RVDefaultHandler // 15 j RVDefaultHandler // 16 j RVDefaultHandler // 17 j RVDefaultHandler // 18 j RVDefaultHandler // 19 j RVDefaultHandler // 20 j RVDefaultHandler // 21 j RVDefaultHandler // 22 j RVDefaultHandler // 23 j RVDefaultHandler // 24 j RVDefaultHandler // 25 RVDefaultHandler: j RVDefaultHandler .section .text .option norvc handle_reset: la t0, trap_vector addi t0, t0, 1 csrw mtvec, t0 csrwi mstatus, 0 csrwi mie, 0 la gp, _gp la sp, __stack_top # clear bss section clear_bss: la t0, __bss_begin la t1, __bss_end li t2, 0x00000000 clear_sbss_loop: sw t2, (t0) addi t0, t0, 4 blt t0, t1, clear_sbss_loop jump_to_main: j main