Skip to content

Commit

Permalink
gcc effort
Browse files Browse the repository at this point in the history
  • Loading branch information
bieganski committed Sep 28, 2023
1 parent 9f5448f commit 5b077f7
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
asm.o
asm.S
*.vcd
xpack-riscv-none-embed-gcc-*
riscv-none-*
*.swp
__pycache__/
**/__pycache__/
Expand Down
21 changes: 18 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DOCKER_IMAGE_NAME := docker.io/library/mtkcpu:1.0.0

lint:
poetry run black .
poetry run isort .
Expand Down Expand Up @@ -26,11 +28,24 @@ build-docker:
bash ./build_docker_image.sh

test-docker:
docker run docker.io/library/mtkcpu:1.0.0 poetry run mtkcpu tests cpu
docker run $(DOCKER_IMAGE_NAME) poetry run mtkcpu tests cpu

run-docker-it:
docker run -it docker.io/library/mtkcpu:1.0.0 bash
docker run -it $(DOCKER_IMAGE_NAME) bash

fetch-gcc: export id := $(shell docker create $(DOCKER_IMAGE_NAME))
fetch-gcc: export temp := $(shell mktemp -p .)
fetch-gcc:
rm -rf riscv-none-elf-gcc
docker cp $(id):/root/.local/xPacks/@xpack-dev-tools/riscv-none-elf-gcc/ - > $(temp)
docker rm -v $(id)
tar xvf $(temp)
rm $(temp)
chmod -R +wx riscv-none-elf-gcc
@echo "== GCC downloaded from docker to host - run the following command to have it in your PATH:"
@echo 'export PATH=$$PATH:$(shell realpath riscv-none-elf-gcc/13.2.0-1.2/.content/bin)'
@echo '======'

test:
poetry run pytest -n 12

21 changes: 0 additions & 21 deletions install_toolchain.sh

This file was deleted.

10 changes: 7 additions & 3 deletions mtkcpu/asm/asm_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

def dump_asm_to_S_file(
val_lst: List[int],
toolchain: str,
filename: str = "asm.S",
verbose: bool = False,
):
if not toolchain.endswith("-"):
toolchain = toolchain + "-"
if os.path.isfile(filename):
os.remove(filename)
with open(filename, "w") as f:
Expand All @@ -27,10 +30,10 @@ def dump_asm_to_S_file(

obj_filename = f"{filename.split('.')[-2]}.o"
subprocess.getoutput(
f"riscv-none-embed-gcc -c {filename} -o {obj_filename}"
f"{toolchain}gcc -c {filename} -o {obj_filename}"
)
output = subprocess.getoutput(
f"riscv-none-embed-objdump -d {obj_filename}"
f"{toolchain}objdump -d {obj_filename}"
)
LOG(output, verbose=verbose)

Expand All @@ -53,6 +56,7 @@ def LOG(*args, **kwargs):
# returns bytearray with asm.
def dump_asm(
code_input: str,
toolchain: str,
verbose: bool = False,
) -> List[int]:

Expand All @@ -62,7 +66,7 @@ def dump_asm(
code = obj.get_section("code").data
code = bytes_to_u32_arr(code)
dump_instrs(code)
dump_asm_to_S_file(code, verbose=verbose)
dump_asm_to_S_file(code, toolchain, verbose=verbose)
return code


Expand Down
19 changes: 10 additions & 9 deletions mtkcpu/tests/test_jtag.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from pathlib import Path
import logging

from shutil import which
import pytest

from mtkcpu.utils.tests.utils import assert_jtag_test
from mtkcpu.units.debug.impl_config import TOOLCHAIN


def get_git_root() -> Path:
Expand All @@ -19,21 +20,21 @@ def get_git_root() -> Path:
def test_openocd_gdb():
logging.info("JTAG test (with openocd and gdb)")

# openocd_executable = get_git_root() / "openocd_riscv" / "src" / "openocd"
# TODO https://github.com/bieganski/mtkcpu/issues/30
# Needs setup&build support for setup stage, to install correct tools.
# TODO: We really need setup&build support for setup stage, to install correct tools.
openocd_executable = get_git_root() / ".." / "riscv-openocd" / "src" / "openocd"

gdb_executable = get_git_root() / "xpack-riscv-none-embed-gcc-8.3.0-2.3" / "bin" / "riscv-none-embed-gdb"
if not openocd_executable.exists():
raise ValueError(f"openocd executable ({openocd_executable}) does not exists!")

gdb_executable = f"{TOOLCHAIN}-gdb"

for x in openocd_executable, gdb_executable:
if not x.exists():
raise ValueError(f"{x} executable does not exists!")
if which(gdb_executable) is None:
raise ValueError(f"gdb executable ({gdb_executable}) either not found or not eXecute permissions")

assert_jtag_test(
with_checkpoints=True,
openocd_executable=openocd_executable,
gdb_executable=gdb_executable,
gdb_executable=Path(which(gdb_executable)),
)

if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions mtkcpu/units/debug/impl_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
DATASIZE = 2
PROGBUF_MMIO_ADDR = 0xde88

TOOLCHAIN = "riscv-none-elf"
GCC_MARCH = "rv32i_zicsr"
22 changes: 8 additions & 14 deletions mtkcpu/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def read_elf(elf_path, verbose=False):

if verbose:
import subprocess
p = subprocess.Popen(["riscv-none-embed-objdump", "--disassembler-options=no-aliases", "-M", "numeric", "-d", elf_path], stdout=subprocess.PIPE)
p = subprocess.Popen(["riscv-none-elf-objdump", "--disassembler-options=no-aliases", "-M", "numeric", "-d", elf_path], stdout=subprocess.PIPE)
out, _ = p.communicate()
out = str(out.decode("ascii"))
raise ValueError(out)
Expand All @@ -106,27 +106,21 @@ def read_elf(elf_path, verbose=False):

# TODO pass additional param
def compile_source(source_raw : str, output_elf : Path, mem_size_kb: int):

from mtkcpu.units.debug.impl_config import TOOLCHAIN, GCC_MARCH

COMPILERS = ["riscv-none-embed-gcc", "riscv-none-elf-gcc"]
for candicate in COMPILERS:
if which(candicate) is not None:
break
else:
raise ValueError(f"Could not find a suitable compiler! Seeked for {COMPILERS}")

compiler = candicate
compiler = f"{TOOLCHAIN}-gcc"

if which(compiler) is None:
raise ValueError(f"Could not find {compiler}")

with NamedTemporaryFile(suffix=".S", delete=False, mode="w+") as asm_file:
assert asm_file.write(source_raw)
with NamedTemporaryFile(suffix=".ld", delete=False) as ld_file:
from mtkcpu.utils.linker import write_linker_script
write_linker_script(Path(ld_file.name), mem_addr=CODE_START_ADDR, mem_size_kb=mem_size_kb)

march = "rv32i"
if "elf" in compiler:
march += "_zicsr"

cmd = [compiler, f"-march={march}", "-mabi=ilp32", "-nostartfiles", f"-T{ld_file.name}", asm_file.name, "-o", output_elf]
cmd = [compiler, f"-march={GCC_MARCH}", "-mabi=ilp32", "-nostartfiles", f"-T{ld_file.name}", asm_file.name, "-o", output_elf]
logging.critical(" ".join(cmd))
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
Expand Down
7 changes: 6 additions & 1 deletion mtkcpu/utils/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from mtkcpu.utils.tests.dmi_utils import *
from mtkcpu.utils.misc import get_color_logging_object
from mtkcpu.cpu.cpu import CPU_Config
from mtkcpu.units.debug.impl_config import TOOLCHAIN


from ppci.arch.riscv import instructions
Expand Down Expand Up @@ -200,7 +201,11 @@ def reg_test(

def get_code_mem(case: MemTestCase, mem_size_kb: int) -> MemoryContents:
if case.source_type == MemTestSourceType.TEXT:
code = dump_asm(case.source, verbose=False)
code = dump_asm(
code_input=case.source,
toolchain=TOOLCHAIN,
verbose=False
)
return MemoryContents(
memory=dict(zip(count(CODE_START_ADDR, 4), code)),
)
Expand Down
2 changes: 1 addition & 1 deletion run_jtag_test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

pkill -f riscv-none-embed-gdb
pkill -f riscv-none-elf-gdb
pkill --signal SIGUSR1 -f openocd

# poetry run pytest mtkcpu/tests/test_jtag.py --verbose --capture=no
Expand Down
4 changes: 2 additions & 2 deletions sw/common/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ endif
# vpath %.cc ../bsp/


TOOLCHAIN := riscv-none-embed-
TOOLCHAIN := riscv-none-elf-
CC := $(TOOLCHAIN)g++
LD := $(TOOLCHAIN)ld

MARCH := rv32i
MARCH := rv32i_zicsr
ARCH_FLAGS := -march=$(MARCH) -mabi=ilp32 -DUSE_GP

GIT_ROOT := $(shell git rev-parse --show-toplevel)
Expand Down

0 comments on commit 5b077f7

Please sign in to comment.