Skip to content

Commit 4ca9aef

Browse files
riscv single cycle cpu with separate IMEM and DMEM working
1 parent fba3c86 commit 4ca9aef

File tree

8 files changed

+267
-128
lines changed

8 files changed

+267
-128
lines changed

examples/risc-v/gcc_test/Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ dump-comment: all
1616
objdump -s --section .comment main
1717

1818
hex: all
19-
./elf2bin.py main mem_init.h
20-
19+
#./elf2bin.py main mem_init.h
20+
riscv32-unknown-elf-objcopy -O binary main text.bin --only-section .text
21+
./bin2vhdl.py text.bin
22+
riscv32-unknown-elf-objcopy -O binary main data.bin --only-section .data
23+
./bin2vhdl.py data.bin
24+
2125
clean:
2226
rm -f main
2327

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
3+
# Parse .bin files output from commands like
4+
# riscv32-unknown-elf-objcopy -O binary main text.bin --only-section .text
5+
# into VHDL array of u32 init string
6+
7+
import sys
8+
import struct
9+
10+
# VHDL doesnt accept DECIMAL 32b unsigned value constants
11+
# since sees as signed 33b integer w/sign bit=0
12+
# so instead explictly convert 32b unsigned to 32b signed
13+
# Thanks https://stackoverflow.com/questions/24563786/conversion-from-hex-to-signed-dec-in-python
14+
def s32(value):
15+
return -(value & 0x80000000) | (value & 0x7fffffff)
16+
17+
in_file = sys.argv[1]
18+
19+
fin = open(in_file, "rb")
20+
name = in_file.strip(".bin")
21+
22+
four_bytes = fin.read(4)
23+
word_count = 0
24+
s = f'#define {name}_MEM_INIT "(\\n\\\n'
25+
while len(four_bytes)==4:
26+
word_count += 1
27+
LITTLE_END_INTEGER = '<i'
28+
the_int = struct.unpack(LITTLE_END_INTEGER, four_bytes)[0]
29+
word_str = f"unsigned(to_signed({str(s32(the_int))}, 32))"
30+
word_str += ",\\n\\\n"
31+
s += word_str
32+
four_bytes = fin.read(4)
33+
34+
s += "others => (others => '0')\\n\\\n"
35+
s += ')"\n'
36+
#s += f'#define MEM_INIT_SIZE {word_count*4}\n'
37+
with open(name+"_mem_init.h", "w") as of:
38+
of.write(s)
39+
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# It does not appear possible to specify
2-
# bootstrap assembly directly in asm("...") C code.
1+
# It require some work to do bootstrap for C code it seems...
32
# For that reason setup that occurs
43
# before the C code entry point is done here
54

@@ -12,10 +11,4 @@
1211
# And then start user main()
1312
_bootstrap:
1413
la sp, stack_end
15-
jal main
16-
17-
# Declare a stack area of memory?
18-
# (must be after entry point? or last in order?)
19-
stack_begin:
20-
.zero 256
21-
stack_end:
14+
jal main

examples/risc-v/gcc_test/link.ld

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,49 @@ https://github.com/agra-uni-bremen/microrv32/blob/master/microrv32/sw/basic-c/li
44
OUTPUT_ARCH( "riscv" )
55
ENTRY(_bootstrap)
66

7-
SECTIONS
7+
/* and Discord user dutracgi,
8+
https://interrupt.memfault.com/blog/how-to-write-linker-scripts-for-firmware */
9+
10+
/* Needs to match mem_map.h (TODO how to share variables?) */
11+
MEMORY
812
{
9-
/* Place entry point at address 0*/
10-
. = 0;
11-
.text.init : { *(.text.init) }
13+
rom (rx) : ORIGIN = 0x00000000, LENGTH = 65536
14+
ram (rw) : ORIGIN = 0x40000000, LENGTH = 65536
1215
}
1316

17+
STACK_SIZE = 0x0100;
18+
19+
/* Section Definitions */
20+
SECTIONS
21+
{
22+
.text :
23+
{
24+
KEEP(*(.vectors .vectors.*))
25+
*(.text*)
26+
*(.rodata*)
27+
} > rom
1428

29+
/* .bss section which is used for uninitialized data */
30+
.bss (NOLOAD) :
31+
{
32+
*(.bss*)
33+
*(COMMON)
34+
} > ram
35+
36+
.data :
37+
{
38+
*(.data*);
39+
} > ram AT >rom
40+
41+
/* stack section */
42+
.stack (NOLOAD):
43+
{
44+
. = ALIGN(4);
45+
stack_begin = .; /* stack base is here */
46+
. = . + STACK_SIZE;
47+
. = ALIGN(4);
48+
stack_end = .; /* stack top is here <- sp */
49+
} > ram
50+
51+
_end = . ;
52+
}

examples/risc-v/gcc_test/mem_map.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@
1919
// TODO just mem map one big struct that gets changes
2020
// needs more efficient code for muxing mem reads/writes to specific struct bytes
2121

22-
#define MEM_MAP_BASE_ADDR 0x10000000
22+
// Needs to match link.ld (TODO how to share variables?)
23+
#define DMEM_ADDR_BIT_CHECK 30
24+
#define DMEM_BASE_ADDR ((uint32_t)((uint32_t)1<<DMEM_ADDR_BIT_CHECK))
25+
#define DMEM_SIZE 65536 // Must be decimal constant since VHDL+C literal
26+
#define IMEM_SIZE 65536 // Must be decimal constant since VHDL+C literal
27+
// Any addresses this high up will be mmio
28+
#define MEM_MAP_ADDR_BIT_CHECK 31
29+
#define MEM_MAP_BASE_ADDR ((uint32_t)((uint32_t)1<<MEM_MAP_ADDR_BIT_CHECK))
2330

2431
#define N_THREADS_PER_BARREL 5
2532
#define N_BARRELS 1
@@ -467,7 +474,7 @@ typedef struct dataflow_mm_t
467474
#define DATAFLOW_MM_ADDR (KERNEL_VALID_OUT_ADDR + sizeof(uint32_t))
468475
static volatile dataflow_mm_t* DATAFLOW_MM = (dataflow_mm_t*)DATAFLOW_MM_ADDR;
469476

470-
void do_shader_dataflow(uint32_t addr, int32_t x, int32_t y, uint32_t frame_count, uint32_t num_pixels){
477+
/*void do_shader_dataflow(uint32_t addr, int32_t x, int32_t y, uint32_t frame_count, uint32_t num_pixels){
471478
// Configure dataflow network
472479
DATAFLOW_MM->addr = addr;
473480
DATAFLOW_MM->x = x;
@@ -482,4 +489,4 @@ void do_shader_dataflow(uint32_t addr, int32_t x, int32_t y, uint32_t frame_coun
482489
// Clear valid to reset
483490
DATAFLOW_MM->all_ports_dataflow_host = 0;
484491
DATAFLOW_MM->valid = 0;
485-
}
492+
}*/

0 commit comments

Comments
 (0)