Coder Social home page Coder Social logo

inpyjama / c-ninja-listings Goto Github PK

View Code? Open in Web Editor NEW
78.0 7.0 343.0 3.25 MB

Lower level assembly and C baremetal programming on RISC-V CPUs. Source code listings from the C-Ninja, in Pyjama! book.

Home Page: https://inpyjama.com

Dockerfile 24.60% Shell 14.90% Makefile 37.46% Assembly 5.41% Batchfile 13.49% C 4.15%
assembly-language baremetal c linker-script makefile risc-v riscv32

c-ninja-listings's Introduction

The code provided as part of this repository is ONLY for learning purposes!

Code Listings for "C-Ninja, in Pyjama!"

This repository contains code listings cited as examples from the book "C Ninja, in Pyjama!". There is also supporting YouTube playlist that guides through the chapters and concepts.

Watch the video Embedded Systems 101: C Ninja, in Pyjama! The right way to learn C...

Environment Setup

All the examples are based on the RISC-V CPU implementing the rv32i instruction set. To avoid having to procure/purchase new hardware, the examples are based on the QEMU emulator that emulates the intended CPU. This makes it easy to learn all the core concepts first before trying to execute them on a real hardware.

To ensure that everyone has the same development environment, we create and use a docker container. Following steps guide on setting up the right docker container locally.

1. Setup Docker Container

You would need to have Docker engine/desktop installed and running. If you don't already have it, get a copy and install it for your Operating system by following steps here: Docker.

1.1. Mac OS

Execute the following to run the setup after Docker is installed and actively running on your local machine.

bash setup.sh

1.1.1. Testing the container

Example of correctly setup environment...

Open a new terminal window. If you are using the bash shell then simply execute the following:

c-ninja

Important: If you are using some other shell, please change to the bash shell first by executing the bash command! Once you are in the bash shell is activated, execute the c-ninja command.

Once within the docker container, execute the following:

c-ninja@c-ninja-linux ~/Documents
% cd test
c-ninja@c-ninja-linux ~/Documents/test
% make

You should see prints as below:

riscv64-unknown-elf-gcc  -O0 -ggdb -nostdlib -march=rv32i -mabi=ilp32 -Wl,-Tmain.ld main.s -o main.elf
riscv64-unknown-elf-objcopy -O binary main.elf main.bin
xxd -e -c 4 -g 4 main.bin
00000000: 00100093  ....

1.2. Linux

The installation of docker is described in detail on the official Docker website: Docker installation on Linux. We cover the steps needed to enable Docker Desktop on Ubuntu Desktop.

1.2.1 Installing Docker Desktop

Installing docker Desktop on Linux is not straight forward when compared to Mac and Windows.

1.2.1.1 Update APT sources

Open a terminal and update the apt sources by executing the following commands:

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

1.2.1.2 Install Docker CLI

Install the dependencies along with docker-ce-cli.

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin curl qemu-system-x86 pass uidmap

1.2.1.3 Install Docker Desktop

Download the Docker Desktop Installer. This should download the docker-desktop-4.24.0-amd64.deb file. From the location where this file is downloaded, execute the following command to install the Docker Desktop -

sudo dpkg -i docker-desktop-4.24.0-amd64.deb

1.2.1.4 Fix the launch of Docker Desktop

sudo rm /etc/xdg/systemd/user/docker-desktop.service
systemctl --user enable docker-desktop

1.2.2 Create the Image and the Container

Ensure Docker Desktop is running and from within the directory for this current repository run the following:

bash setup.sh

This will create the image and the container.

1.2.3 Testing Installation

Open a new terminal window. If you are using the bash shell then simply execute the following:

c-ninja

1.2.3.1. Linux Host Only: Change Ownership of the directory!

Once within the docker container, you will need to change the ownership of the contents within ~/Documents/ in the container. Execute the command as shown below:

c-ninja@c-ninja-linux ~/Documents
% sudo chown -R c-ninja .

This will change the ownership of the files to the local user c-ninja within the docker container. After this you should be able to issue cd test; make as below -

c-ninja@c-ninja-linux ~/Documents
% cd test
c-ninja@c-ninja-linux ~/Documents/test
% make

This should result in prints as below, confirming that the environment is set correctly.

riscv64-unknown-elf-gcc  -O0 -ggdb -nostdlib -march=rv32i -mabi=ilp32 -Wl,-Tmain.ld main.s -o main.elf
riscv64-unknown-elf-objcopy -O binary main.elf main.bin
xxd -e -c 4 -g 4 main.bin
00000000: 00100093  ....

1.3. Windows

Execute the following to run the setup after Docker is installed and actively running on your local machine. You can either double-click this file or execute it from the cmd/power shell prompt. This command has been tested on Windows 11.

setup.bat

These commands should download the required Linux image, create a container using it, install all the required packages, create the c-ninja user, and map the current directory as the ~/Documents

1.3.1. Testing the installation

You should see a new, c-ninja.bat file generated in the current directory. You can double-click on the file to launch the terminal connected to the docker container.

2. Setup VScode

code --install-extension ms-vscode-remote.remote-containers
code --install-extension ms-vscode-remote.remote-ssh
code --install-extension ms-vscode-remote.remote-ssh-edit
code --install-extension ms-vscode.remote-explorer
code --install-extension zhwu95.riscv
code --install-extension ZixuanWang.linkerscript
  • Restart VSCode and from the Remote explorer connect to the container.

Copyright

Copyright © 2023 inpyjama.com. All Rights Reserved.

c-ninja-listings's People

Contributors

dtom7 avatar piyushthange avatar streetdogg avatar therustyengineer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

c-ninja-listings's Issues

Permission Denied Error when Running 'make' in '/Document/test' directory

I'm encountering an issue when trying to run 'make' in the /Document/test directory while following the YouTube video(C-Language: C Ninja, in Pyjama! - #0. Lab Setup!) at 12:44

c-ninja@c-ninja-linux ~/Documents/test
% make
riscv64-unknown-elf-gcc -O0 -ggdb -nostdlib -march=rv32i -mabi=ilp32 -Wl,-Tmain.ld main.s -o main.elf
/usr/lib/riscv64-unknown-elf/bin/ld: cannot open output file main.elf: Permission denied
collect2: error: ld returned 1 exit status

I have also attached the screen of my terminal.
Screenshot from 2023-09-17 12-21-06

Here are the details when I run uname -a in my terminal.
Screenshot from 2023-09-17 12-26-32

I am using ubuntu.
image

trouble with dev container

when i the open remote explorer and try to launch c-ninja-listing in dev containers iget this error[pic attached below]
Screenshot (399)

bash setup.sh fails to complete

Hello,
While trying to execute bash setup.sh i encounter the following problem:

` => ERROR [6/9] RUN apt-get install --no-install-recommends -y ca-certificates sudo gdb-multiarch gcc-riscv64-unknown-elf binutils-common make curl 4.2s

[6/9] RUN apt-get install --no-install-recommends -y ca-certificates sudo gdb-multiarch gcc-riscv64-unknown-elf binutils-common make curl git-core wget nano ssh tree less build-essential python3 python3-dev binutils make binutils-riscv64-linux-gnu qemu-system-riscv32 zsh xxd strace man:
0.809 Reading package lists...
3.514 Building dependency tree...
3.883 Reading state information...
4.093 E: Unable to locate package gcc-riscv64-unknown-elf
4.093 E: Unable to locate package qemu-system-riscv32`

It looks like it can not locate gcc-riscv64-unknown-elf and qemu-system-riscv32.
I am running on Ubuntu 18.04.
Do you have any idea what could be the problem here, might it be the Ubuntu version, the fact that i didn't install the Docker Desktop (i don't think the these should pose any problem by the way) or is it something else of which i am not aware?

Thank you

Error when I enter the docker command

image

I have tried entering the command you mentioned and it does not work should I be doing any kind of setting on the docker side??

I see that c-ninja is running on Docker
image

riscv x0

posting it here for better reach and getting the basics right.

image

vs code error

This is what i am getting in vs code . Kindly help
Screenshot 2024-06-17 003143

Issue in make debug terminal

I am able to successfully close the make gdb terminal
but when I am trying to close make debug terminal with ctrl+a followed by x, nothing is happening
Screenshot from 2023-10-26 10-11-17

pasword

please give the pasword of the c-ninja

signed vs unsigned char

if Char is used for representing ASCII symbols, what is the significance of having a signed and unsigned 'char'?. does a signed and unsigned char point to a different symbol or the same symbol?. if they both represent the same symbol why the extra keyword usage?

Understanding cores in microprocessor

Hello,
I have a doubt
Cortex M4 has 4 cores in one processor
Infeion AURIX controller has 4 cores which they call 4 different processor

could you please explain how 4 cores in a processor and 4 different processor in one microcontroller is different?

Issue in make debug

issue_exit_debug

may be I failed to exit debug or don't understand what happend not able to make debug now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.