{"id":15060442,"url":"https://github.com/inpyjama/c-ninja-listings","last_synced_at":"2025-04-10T16:11:55.238Z","repository":{"id":189526550,"uuid":"680377867","full_name":"inpyjama/c-ninja-listings","owner":"inpyjama","description":"Lower level assembly and C baremetal programming on RISC-V CPUs. Source code listings from the C-Ninja, in Pyjama! book.","archived":false,"fork":false,"pushed_at":"2024-10-23T10:34:55.000Z","size":3325,"stargazers_count":1,"open_issues_count":8,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T14:01:27.691Z","etag":null,"topics":["assembly-language","baremetal","c","linker-script","makefile","risc-v","riscv32"],"latest_commit_sha":null,"homepage":"https://inpyjama.com","language":"Makefile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inpyjama.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-19T03:53:54.000Z","updated_at":"2025-03-12T01:29:07.000Z","dependencies_parsed_at":"2024-02-11T04:24:06.516Z","dependency_job_id":"bba8b557-ffaf-4205-b3db-e3c1db01d1ac","html_url":"https://github.com/inpyjama/c-ninja-listings","commit_stats":null,"previous_names":["in-pyjama/c-ninja-listings","inpyjama/c-ninja-listings"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inpyjama%2Fc-ninja-listings","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inpyjama%2Fc-ninja-listings/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inpyjama%2Fc-ninja-listings/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inpyjama%2Fc-ninja-listings/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inpyjama","download_url":"https://codeload.github.com/inpyjama/c-ninja-listings/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248251020,"owners_count":21072685,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assembly-language","baremetal","c","linker-script","makefile","risc-v","riscv32"],"created_at":"2024-09-24T22:58:45.937Z","updated_at":"2025-04-10T16:11:55.222Z","avatar_url":"https://github.com/inpyjama.png","language":"Makefile","funding_links":[],"categories":[],"sub_categories":[],"readme":"```\nThe code provided as part of this repository is ONLY for learning purposes!\n```\n\n# Code Listings for \"C-Ninja, in Pyjama!\"\n\nThis 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.\n\n\u003cfigure\u003e\n  \u003ca href=\"https://www.youtube.com/playlist?list=PLFt5JBAXXlQow0PLTdqvaiIUMRa71VQHl\" target=\"_blank\"\u003e\n    \u003cimg src=\"imgs/youtube.png\" alt=\"Watch the video\"/\u003e\n    \u003cfigcaption align = \"center\"\u003eEmbedded Systems 101: C Ninja, in Pyjama! The right way to learn C...\u003c/figcaption\u003e\n  \u003c/a\u003e\n\u003c/figure\u003e\n\n\n# Environment Setup\n\nAll 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.\n\nTo 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.\n\n## 1. Setup Docker Container\n\nYou 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](https://www.docker.com).\n\n### 1.1. Mac OS\n\nExecute the following to run the setup after [Docker](https://www.docker.com) is installed and actively running on your local machine.\n\n```bash\nbash setup.sh\n```\n\n#### 1.1.1. Testing the container\n\n\u003cfigure\u003e\n  \u003cimg src=\"imgs/c-ninja.png\"\u003e\n  \u003cfigcaption align = \"center\"\u003eExample of correctly setup environment...\u003c/figcaption\u003e\n\u003c/figure\u003e\n\nOpen a new terminal window. If you are using the `bash` shell then simply execute the following:\n```bash\nc-ninja\n```\n\n\u003cb style=\"color:red\"\u003eImportant:\u003c/b\u003e 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.\n\nOnce within the docker container, execute the following:\n\n```bash\nc-ninja@c-ninja-linux ~/Documents\n% cd test\nc-ninja@c-ninja-linux ~/Documents/test\n% make\n```\n\nYou should see prints as below:\n\n```bash\nriscv64-unknown-elf-gcc  -O0 -ggdb -nostdlib -march=rv32i -mabi=ilp32 -Wl,-Tmain.ld main.s -o main.elf\nriscv64-unknown-elf-objcopy -O binary main.elf main.bin\nxxd -e -c 4 -g 4 main.bin\n00000000: 00100093  ....\n```\n\n## 1.2. Linux\n\nThe installation of docker is described in detail on the official Docker website: [Docker installation on Linux](https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository). We cover the steps needed to enable Docker Desktop on Ubuntu Desktop.\n\n#### 1.2.1 Installing Docker Desktop\n\nInstalling docker Desktop on Linux is not straight forward when compared to Mac and Windows.\n#### 1.2.1.1 Update APT sources\n\nOpen a terminal and update the `apt` sources by executing the following commands:\n```bash\n# Add Docker's official GPG key:\nsudo apt-get update\nsudo apt-get install ca-certificates curl gnupg\nsudo install -m 0755 -d /etc/apt/keyrings\ncurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg\nsudo chmod a+r /etc/apt/keyrings/docker.gpg\n\n# Add the repository to Apt sources:\necho \\\n  \"deb [arch=\"$(dpkg --print-architecture)\" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \\\n  \"$(. /etc/os-release \u0026\u0026 echo \"$VERSION_CODENAME\")\" stable\" | \\\n  sudo tee /etc/apt/sources.list.d/docker.list \u003e /dev/null\nsudo apt-get update\n```\n\n#### 1.2.1.2 Install Docker CLI\n\nInstall the dependencies along with `docker-ce-cli`.\n\n```bash\nsudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin curl qemu-system-x86 pass uidmap\n```\n\n#### 1.2.1.3 Install Docker Desktop\n\nDownload the [Docker Desktop Installer](https://desktop.docker.com/linux/main/amd64/docker-desktop-4.24.0-amd64.deb?utm_source=docker\u0026utm_medium=webreferral\u0026utm_campaign=docs-driven-download-linux-amd64). 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 -\n\n\n```bash\nsudo dpkg -i docker-desktop-4.24.0-amd64.deb\n```\n\n#### 1.2.1.4 Fix the launch of Docker Desktop\n```bash\nsudo rm /etc/xdg/systemd/user/docker-desktop.service\nsystemctl --user enable docker-desktop\n```\n\n### 1.2.2 Create the Image and the Container\n\nEnsure Docker Desktop is running and from within the directory for this current repository run the following:\n\n```bash\nbash setup.sh\n```\n\nThis will create the image and the container.\n\n#### 1.2.3 Testing Installation\nOpen a new terminal window. If you are using the `bash` shell then simply execute the following:\n```bash\nc-ninja\n```\n\n#### \u003cb style=\"color:red\"\u003e1.2.3.1. Linux Host Only: Change Ownership of the directory!\u003c/b\u003e\n\nOnce 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:\n\n```bash\nc-ninja@c-ninja-linux ~/Documents\n% sudo chown -R c-ninja .\n```\n\nThis 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 -\n\n```bash\nc-ninja@c-ninja-linux ~/Documents\n% cd test\nc-ninja@c-ninja-linux ~/Documents/test\n% make\n```\n\nThis should result in prints as below, confirming that the environment is set correctly.\n\n```bash\nriscv64-unknown-elf-gcc  -O0 -ggdb -nostdlib -march=rv32i -mabi=ilp32 -Wl,-Tmain.ld main.s -o main.elf\nriscv64-unknown-elf-objcopy -O binary main.elf main.bin\nxxd -e -c 4 -g 4 main.bin\n00000000: 00100093  ....\n```\n\n### 1.3. Windows\n\nExecute the following to run the setup after [Docker](https://www.docker.com) 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.\n\n```bash\nsetup.bat\n```\n\nThese 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`\n\n#### 1.3.1. Testing the installation\n\nYou 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.\n\n## 2. Setup VScode\n- [Download and Install Vscode](https://code.visualstudio.com/download) for your operating system.\n- Launch VScode, open the terminal, and install the extensions -\n```bash\ncode --install-extension ms-vscode-remote.remote-containers\ncode --install-extension ms-vscode-remote.remote-ssh\ncode --install-extension ms-vscode-remote.remote-ssh-edit\ncode --install-extension ms-vscode.remote-explorer\ncode --install-extension zhwu95.riscv\ncode --install-extension ZixuanWang.linkerscript\n```\n- Restart VSCode and from the Remote explorer connect to the container.\n\n# Copyright\n\nCopyright © 2023 inpyjama.com. All Rights Reserved.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finpyjama%2Fc-ninja-listings","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finpyjama%2Fc-ninja-listings","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finpyjama%2Fc-ninja-listings/lists"}