y2kbugger / y2kbugger.github.io

y2kbugger.com
0 stars 0 forks source link

baremetal-riscv-renode-1 #2

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Baremetal RISC-V Renode - Part 1: Blinky - y2kbugger

https://blog.y2kbugger.com/baremetal-riscv-renode-1.html

y2kbugger commented 3 years ago

For a build environment aimed more at being useful, than for learning, also checkout https://github.com/timvideos/litex-buildenv it incorporates both hardware toolchains (Compiling softcores e.g. risc-v) and software toolchains. It can either run within renode or push to a real FPGA.

y2kbugger commented 3 years ago

See also: https://twilco.github.io/riscv-from-scratch/2019/03/10/riscv-from-scratch-1.html

bat52 commented 1 year ago

Thank you very much for this useful guide! I am usually not scared by complex startup, but I must admit that getting renode to work is proving much harder than I thought. I have already been able to compile the riscv toolchain on my own, and even build a poor's man riscv profiler (https://github.com/bat52/rv8_prof), but what I really want now is to be able to do some baremetal embedded development, and over time be able to integrate with verliated peripherals for SW/HW codesign. I had been fiddling with renode's examples for a while, and never managed to get a sw locally compiled properly. I was hoping not having to re-compile renode, but I am getting "[ERROR] cpu: RISC-V 'C' instruction set is not enabled for this CPU! PC: 0x1010, opcode: 0x0 (309019)", so I guess I should probably. Bummer. It feels like having a VM or a container image to quickstart this whole process might greatly help!

bat52 commented 1 year ago

Oh, and BTW thanks for the link about Miro Samek! This reminded me I had a Stellaris board laying around, and finally get it back to work, and even integrated some old arduino code (https://github.com/bat52/stellaris).

y2kbugger commented 1 year ago

@bat52 Incorporating verilated hardware is my next project!

Regarding your error, the emulator believes you are trying to execute the compact instruction set.

See: https://riscv.org/wp-content/uploads/2015/11/riscv-compressed-spec-v1.9.pdf

This excerpt from the RISC-V User-Level ISA Specification describes the current draft proposal for the RISC-V standard compressed instruction set extension, named “C”, which reduces static and dynamic code size by adding short 16-bit instruction encodings for common operations. The C extension can be added to any of the base ISAs (RV32, RV64, RV128), and we use the generic term “RVC” to cover any of these. Typically, 50%–60% of the RISC-V instructions in a program can be replaced with RVC instructions, resulting in a 25%–30% code-size reduction.

Either you are compiling with that extension turned on or, more likely, there is some other misconfiguration where you are trying to execute the wrong bytes, e.g. a data section or section of zeros (notice your opcode: 0x0).

Have you correctly initialized/configured the processor's program counter (pc)? I think renode pulls the PC from the ELF format and "does the right thing" based on where in memory it's loaded. Also make sure you tell the linker where the entrypoint is:

.section .text
.global _start
_start:
bat52 commented 1 year ago

Thank you! You are right, the error is obvious if you read it well. I had to add "-march=rv32i" to the gcc command, and then it worked. Perhaps your build configuration uses that as a default ? Alternatively, looking at renode default files, I found the supported ISA can be specified in the .repl file as below

cpu: CPU.VexRiscv @ sysbus
    cpuType: "rv32imac"

So now it works!

y2kbugger commented 1 year ago

@bat52 check out the gcc cross compile config here: https://github.com/y2kbugger/baremetal-riscv-renode/blob/c227fbcb675a059621073daf3893603adaff868d/Makefile#L10

Also very cool. I did not know that you could enable the extension in vexrisc!

nsantee commented 5 months ago

getting this error when i try to make toolchains. Pretty sure i have everything installed because im able to run the portable in another directory.

ld -lncurses 2> /dev/null || error 'ncurses headers to build gdb with the text user interface enabled.'
cd riscv-gnu-toolchain && ./configure --prefix=/home/labuser/Public/baremetal-riscv-renode/riscv-gnu-toolchain-output --disable-linux --with-arch=rv32i --with-riscv-attribute='yes' --enable-languages=c --enable-tui=yes
/bin/sh: 1: ./configure: not found
make: *** [Makefile:12: riscv-gnu-toolchain-output/bin/riscv32-unknown-elf-gcc] Error 127
y2kbugger commented 5 months ago

Let's check what version of the repo you are on:

git status

Also please be sure that you recursively cloned the repository as per https://blog.y2kbugger.com/baremetal-riscv-renode-1.html#source-code:

git clone --recursive https://github.com/y2kbugger/baremetal-riscv-renode.git
nsantee commented 5 months ago

All good on that step, thanks for your help. I had a download issue, was able to build everything fine, after running a sed -i -e 's/\r$//' activate-toolchains.sh (i think this file might have been saved in windows, and bash wouldnt execute it) and also sudo apt install dotnet-sdk-6.0. So everything on the setup is good now.

Only issue is i drop into 1_blink, and issue a make launch but get make: ** No rule to make target 'src/.s', needed by 'image'. Stop. Any comment on that? Really appreciate your great help!

y2kbugger commented 5 months ago

Feel free to put in pull request for the line ending problem?

Just to make sure you are on a recent version, do you end up with a <BAREMETAL> prompt after activating? image

I have made a lot of progress on the toy operating system "kos" so I broke the make scripts for blinky because i made them share a common file.

I have pushed fixed up (and simplified) Makefiles for 1_blinky and 3_uart the uart is when things get fun. https://github.com/y2kbugger/baremetal-riscv-renode/commit/0a1b50ec27512010cc9fdef6c40b7ff11f355afd

nsantee commented 5 months ago

The .c file seemed to be missing in 1_blinky so i wasnt able to build (see prior comment), but the uart example had the baremetal.c

built, and ended up with this error baremetal.s:11: Error: unrecognized opcode csrw mtvec,t0', extensionzicsr' required.

However in the makefile.common, i see you are issuing -march=rv32i_zicsr

Any idea on why this extension is not found?

y2kbugger commented 5 months ago

The .c file seemed to be missing in 1_blinky so i wasnt able to build

the first example only has assembly, this is reflected in my updated file

I will look into the zicsr thing.

y2kbugger commented 5 months ago

built, and ended up with this error baremetal.s:11: Error: unrecognized opcode csrw mtvec,t0', extensionzicsr' required.

However in the makefile.common, i see you are issuing -march=rv32i_zicsr

What command are you running when you see this error, during build or during simulation? Might be the same prob as fixed here. I don't know why I have never run into this error. https://github.com/y2kbugger/y2kbugger.github.io/issues/2#issuecomment-1332842320

nsantee commented 5 months ago

riscv32-unknown-elf-gcc baremetal.s baremetal.c -ggdb -O0 -o image -ffreestanding -nostdlib

is the command, i get around it, by adding a .option arch, +zicsr to the baremetal.s file (right after start near top). Then gcc runs fine.

however make launch still wont work. getting a make: ** No rule to make target 'src/.s', needed by 'image'. Stop.

so i add a new folder called src, and put the releveant files in there. I get past the error and end up with

make: *** No rule to make target 'src/**/*.c', needed by 'image'. Stop. Any idea?

y2kbugger commented 5 months ago

Did you pull the latest commit?

On Thu, Jan 18, 2024, 20:40 nsantee @.***> wrote:

riscv32-unknown-elf-gcc baremetal.s baremetal.c -ggdb -O0 -o image -ffreestanding -nostdlib

is the command, i get around it, by adding a .option arch, +zicsr to the baremetal.s file (right after start near top). Then gcc runs fine.

however make launch still wont work. getting a *make: ** No rule to make target 'src/.s', needed by 'image'. Stop.*

so i add a new folder called src, and put the releveant files in there. I get past the error and end up with

*make: * No rule to make target 'src//.c', needed by 'image'. Stop.

Any idea?

— Reply to this email directly, view it on GitHub https://github.com/y2kbugger/y2kbugger.github.io/issues/2#issuecomment-1899498236, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABORVLP2KM35WDILMWSKFZLYPHFHJAVCNFSM4VGD5JQKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBZHE2DSOBSGM3A . You are receiving this because you commented.Message ID: @.***>

nsantee commented 5 months ago

yes, pulled everything this afternoon, so should be latest

y2kbugger commented 5 months ago

No, a couple comments ago I told you I fixed all the makefiles about 10 minutes ago. You need to pull them.

y2kbugger commented 5 months ago

Screenshot_20240118-204436 Problem was I tried to share make file.... To complex I just correct over for each example.

nsantee commented 5 months ago

Thanks much!! Apologies for missing your remark about the update today. Ok, looks like I'm almost good now. Monitor fires up, then emulation starts. Then end up with some unknown field "SetPerformance", seen this one before?

This is a great example and i really appreciate your support for it.

17:50:01.5265 [INFO] Loaded monitor commands from: /home/labuser/Public/renode_baremetal/baremetal/renode/scripts/monitor.py 17:50:01.5332 [WARNING] On dotnet configuration option "collapse-repeated-log-entries" is not supported in console mode on unix-like system. It will be set to false.

(monitor) i $CWD/vexriscv.resc 17:50:01.7811 [INFO] Including script: /home/labuser/Public/renode_baremetal/baremetal/3_uart/vexriscv.resc 17:50:01.7992 [INFO] System bus created. 17:50:03.1100 [INFO] sysbus: Loading segment of 4800 bytes length at 0x10000. 17:50:03.1515 [INFO] cpu: Setting PC value to 0x10080. 17:50:03.1876 [INFO] vexriscv-machine: GDB server with all CPUs started on port :3335 Starting emulation... 17:50:03.1945 [INFO] vexriscv-machine: Machine started. There was an error executing command 'emulation SetPerformance 0.001' emulation does not provide a field, method or property SetPerformance. (vexriscv-machine)

nsantee commented 5 months ago

gpio toggle example working perfectly now. there is a "SetPerformance" field not recognized in the other, probably more of a warning because the emulation is running both cases.

y2kbugger commented 5 months ago

gpio toggle example working perfectly now. there is a "SetPerformance" field not recognized in the other, probably more of a warning because the emulation is running both cases.

Awesome!!

y2kbugger commented 5 months ago

Yea if i recall correctly SetPerformance was deprecated and removed from renode last year. I used it to make the blink happen at a reasonable speed. Feel free to submit 2 separate pull requests if you want to contribute to my little project:

  1. fix the line endings in the toolchain script if they are indeed messed up.
  2. remove SetPerformance from anywhere in blinky or uart
nsantee commented 5 months ago

Ok, would be happy to contribute, i'm looking for a branch I can commit to, then submit a pull request for your merge to master. It seems i do not have branch permissions... If there exists a write permissive branch from master @ head revision, then I will clone\push origin listed changes to said branch then submit a PR.

But I might be missing something, maybe there is an easier way, please advise. Thanks for your help.

y2kbugger commented 5 months ago

Here is a good point on forking and pull requests

nsantee commented 5 months ago

Thanks, can you add the License information to the readme.md (or is it somewhere else)? Its not clear what license this codebase is using. Much appreciated!

y2kbugger commented 5 months ago

https://github.com/y2kbugger/baremetal-riscv-renode/blob/master/LICENSE

Added

nsantee commented 5 months ago

Is there a reason for gplv3 vs. MIT as renode itself has? My group's policy may hamper efforts for me to push : (

On Fri, Jan 19, 2024, 11:42 AM Zak Kohler @.***> wrote:

https://github.com/y2kbugger/baremetal-riscv-renode/blob/master/LICENSE

Added

— Reply to this email directly, view it on GitHub https://github.com/y2kbugger/y2kbugger.github.io/issues/2#issuecomment-1901005539, or unsubscribe https://github.com/notifications/unsubscribe-auth/BFOIT57SL7ZV3HC52A7PRKLYPLEBPAVCNFSM4VGD5JQKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJQGEYDANJVGM4Q . You are receiving this because you commented.Message ID: @.***>

y2kbugger commented 5 months ago

It prevents derived works from being proprietary, e.g. it encourages sharing.

On Fri, Jan 19, 2024 at 3:16 PM nsantee @.***> wrote:

Is there a reason for gplv3 vs. MIT as renode itself has? My group's policy may hamper efforts for me to push : (

On Fri, Jan 19, 2024, 11:42 AM Zak Kohler @.***> wrote:

https://github.com/y2kbugger/baremetal-riscv-renode/blob/master/LICENSE

Added

— Reply to this email directly, view it on GitHub < https://github.com/y2kbugger/y2kbugger.github.io/issues/2#issuecomment-1901005539>,

or unsubscribe < https://github.com/notifications/unsubscribe-auth/BFOIT57SL7ZV3HC52A7PRKLYPLEBPAVCNFSM4VGD5JQKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJQGEYDANJVGM4Q>

. You are receiving this because you commented.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/y2kbugger/y2kbugger.github.io/issues/2#issuecomment-1901048362, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABORVLPVJODE3BZNWTQ6SODYPLIARAVCNFSM4VGD5JQKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJQGEYDIOBTGYZA . You are receiving this because you commented.Message ID: @.***>

nsantee commented 5 months ago

It does! But it also prevents corporate folks from using it and pushing, since all derivative work must also be gplv3. Where with MIT, legal departments aren't going to have much a problem with engineers using or (if in good faith) contributing. Renode itself is MIT and this is a major reason for its growth and adoption.

Any gplv3 work deriving from renode becomes basically educational use only, from a corporate perspective.. instead of a clean same-licensed extension that can be used with same considerations as renode.

On Sat, Jan 20, 2024, 2:07 PM Zak Kohler @.***> wrote:

It prevents derived works from being proprietary, e.g. it encourages sharing.

On Fri, Jan 19, 2024 at 3:16 PM nsantee @.***> wrote:

Is there a reason for gplv3 vs. MIT as renode itself has? My group's policy may hamper efforts for me to push : (

On Fri, Jan 19, 2024, 11:42 AM Zak Kohler @.***> wrote:

https://github.com/y2kbugger/baremetal-riscv-renode/blob/master/LICENSE

Added

— Reply to this email directly, view it on GitHub <

https://github.com/y2kbugger/y2kbugger.github.io/issues/2#issuecomment-1901005539>,

or unsubscribe <

https://github.com/notifications/unsubscribe-auth/BFOIT57SL7ZV3HC52A7PRKLYPLEBPAVCNFSM4VGD5JQKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJQGEYDANJVGM4Q>

. You are receiving this because you commented.Message ID: @.***>

— Reply to this email directly, view it on GitHub < https://github.com/y2kbugger/y2kbugger.github.io/issues/2#issuecomment-1901048362>,

or unsubscribe < https://github.com/notifications/unsubscribe-auth/ABORVLPVJODE3BZNWTQ6SODYPLIARAVCNFSM4VGD5JQKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJQGEYDIOBTGYZA>

. You are receiving this because you commented.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/y2kbugger/y2kbugger.github.io/issues/2#issuecomment-1902276235, or unsubscribe https://github.com/notifications/unsubscribe-auth/BFOIT52Q72WNPOX53FRNTX3YPQ53JAVCNFSM4VGD5JQKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJQGIZDONRSGM2Q . You are receiving this because you commented.Message ID: @.***>

Vaseem205 commented 3 months ago

whats the significance of the in the line <jump loop, t0> of asm code?

y2kbugger commented 3 months ago

@Vaseem205 it jumps back to the loop label. Basically making an infinite loop, blinking forever.

vflobo commented 2 months ago

@y2kbugger, Thank you for this wonderful write-up. I was curious to try it on Ubuntu and I must say that I have not played much with it. I was trying to follow your steps exactly as you laid out above. I thin the submodule link is broken for renode-verilator-integration. can you please fix it? the --recursive clone wont work and I am not sure if I missed anything in the process.

I used make launch and I see that some scripts did not really run

This is the error I see:

vijayl@ira:~/baremetal-riscv-renode/1_blinky$ make launch renode --net vexriscv.resc --console /home/vijayl/baremetal-riscv-renode/renode/renode: line 110: dotnet: command not found Any hep is greatly appreciated!
y2kbugger commented 2 months ago

I don't use ubuntu, but try this article: https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu-install?tabs=dotnet8&pivots=os-linux-ubuntu-2404

On Tue, Apr 23, 2024 at 12:41 AM vflobo @.***> wrote:

@y2kbugger https://github.com/y2kbugger, Thank you for this wonderful write-up. I was curious to try it on Ubuntu and I must say that I have not played much with it. I was trying to follow your steps exactly as you laid out above. I thin the submodule link is broken for renode-verilator-integration. can you please fix it? the --recursive clone wont work and I am not sure if I missed anything in the process.

I used make launch and I see that some scripts did not really run

This is the error I see: @.***:~/baremetal-riscv-renode/1_blinky$ make launch renode --net vexriscv.resc --console /home/vijayl/baremetal-riscv-renode/renode/renode: line 110: dotnet: command not found Any hep is greatly appreciated!

— Reply to this email directly, view it on GitHub https://github.com/y2kbugger/y2kbugger.github.io/issues/2#issuecomment-2071395397, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABORVLOHISYVPJJQBEVUHTDY6XQ6NAVCNFSM4VGD5JQKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMBXGEZTSNJTHE3Q . You are receiving this because you were mentioned.Message ID: @.***>

Vaseem205 commented 2 months ago

@y2kbugger Hi.

If I were to implement backspace functionality in the existing shell, how should i go about it?

y2kbugger commented 1 week ago

@y2kbugger, Thank you for this wonderful write-up. I was curious to try it on Ubuntu and I must say that I have not played much with it. I was trying to follow your steps exactly as you laid out above. I thin the submodule link is broken for renode-verilator-integration. can you please fix it? the --recursive clone wont work and I am not sure if I missed anything in the process.

I used make launch and I see that some scripts did not really run

This is the error I see: vijayl@ira:~/baremetal-riscv-renode/1_blinky$ make launch renode --net vexriscv.resc --console /home/vijayl/baremetal-riscv-renode/renode/renode: line 110: dotnet: command not found Any hep is greatly appreciated!

@vflobo Sorry for the late reply, As for the verilator, you don't need that until later but I realized that I have some changes that I haven't upstreamed to that library yet. I will get back to you after I fork it. for now you can remove the dependency on verilator in the makefile Let me know if you get this up and running. I getting into the kos part can be really cool.

y2kbugger commented 1 week ago

@y2kbugger Hi.

If I were to implement backspace functionality in the existing shell, how should i go about it?

@Vaseem205 Sorry for the late reply Ahh this is pretty tricking. this would would mean "cooking" the "raw" input. This is something I have been working on. I want to support ctrl-c to kill current process asynchronously as a signal/interrupt... long story short, i've delayed messing around cooked keyboard input until I have signals implemented.

if you wanted to hack around adding backspace for the current, synchronously only shell, see: https://github.com/y2kbugger/baremetal-riscv-renode/blob/master/kos/src/programs/shell.c

you can see I have backspace defined twice #define KEY_BS '\010' // backspace and a couple of #define BACKSP '\010' so it's really messy/incomplete.

notice here that I'm just playing around (printing xxx) with how to handle escape sequence like arrow keys to move around through existing input and eventually through a history of commands.

https://github.com/y2kbugger/baremetal-riscv-renode/blob/5fc602e0e04b9ea71b912eb96926ef30922148c5/kos/src/programs/shell.c#L207-L242

char *command = command_buffer;
char *read_command()
{
    char *command_end = command;
    printf("kos> ");
    fflush(stdout);

    while (command_end == command)
        while (1)
        {
            int c = getchar();
            if (c == '\n')
                break;
            if (c == SIGSTOP)
                continue;
            if (c == SIGKILL)
                continue;
            if (c == ESCAPE)
            {
                getchar();
                getchar();
                getchar();
                *command_end++ = 'x';
                *command_end++ = 'x';
                *command_end++ = 'x';
                putchar('x');
                putchar('x');
                putchar('x');
                fflush(stdout);
                continue;
            }

            putchar(c);
            fflush(stdout);
            *command_end++ = c;
        }