---
# System prepended metadata

title: 'Assignment3: Construct a RISC-V CPU with Chisel'

---

# Assignment3: Construct a RISC-V CPU with Chisel

## setup (on ubuuntu linux)
$ sudo apt install build-essential verilator gtkwave

$ java -version
openjdk version "11.0.25" 2024-10-15
OpenJDK Runtime Environment (build 11.0.25+9-post-Ubuntu-1ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.25+9-post-Ubuntu-1ubuntu120.04, mixed mode, sharing)

$ javac -version
javac 11.0.25

$ sudo apt update && sudo apt upgrade -y

$ sudo apt install unzip -y

$ sudo apt install zip -y

$ curl -s "https://get.sdkman.io" | bash

$ source "$HOME/.sdkman/bin/sdkman-init.sh"

$ sdk version
SDKMAN!
script: 5.18.2
native: 0.4.6

$ sdk install java 11.0.21-tem

$ sdk use java 11.0.21-tem
Using java version 11.0.21-tem in this shell.




## Install docker on Ubuntu

daniel@MSI:~$ for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'docker.io' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'docker-doc' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'docker-compose' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'docker-compose-v2' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package podman-docker
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'containerd' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'runc' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
daniel@MSI:~$ sudo apt-get update
Get:1 https://download.docker.com/linux/ubuntu focal InRelease [57.7 kB]
Get:2 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [51.8 kB]
Hit:3 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:4 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:5 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:6 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Fetched 109 kB in 2s (52.3 kB/s)
Reading package lists... Done
daniel@MSI:~$ sudo apt-get install ca-certificates curl
Reading package lists... Done
Building dependency tree
Reading state information... Done
ca-certificates is already the newest version (20240203~20.04.1).
curl is already the newest version (7.68.0-1ubuntu2.24).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
daniel@MSI:~$ sudo install -m 0755 -d /etc/apt/keyrings
daniel@MSI:~$ ls -ld /etc/apt/keyrings
drwxr-xr-x 2 root root 4096 Dec  4 21:15 /etc/apt/keyrings
daniel@MSI:~$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
daniel@MSI:~$ ls -l /etc/apt/keyrings/docker.asc
-rw-r--r-- 1 root root 3817 Dec  4 21:19 /etc/apt/keyrings/docker.asc
daniel@MSI:~$ sudo chmod a+r /etc/apt/keyrings/docker.asc
daniel@MSI:~$ echo \
>   "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
>   $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
>   sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
daniel@MSI:~$ cat /etc/apt/sources.list.d/docker.list
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu   focal stable
daniel@MSI:~$ sudo apt-get update
Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [128 kB]
Hit:3 http://archive.ubuntu.com/ubuntu focal InRelease
Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [128 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [128 kB]
Fetched 383 kB in 2s (192 kB/s)
Reading package lists... Done
daniel@MSI:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  docker-ce-rootless-extras pigz slirp4netns
Suggested packages:
  aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
  containerd.io docker-buildx-plugin docker-ce docker-ce-cli
  docker-ce-rootless-extras docker-compose-plugin pigz
  slirp4netns
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 123 MB of archives.
After this operation, 442 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://download.docker.com/linux/ubuntu focal/stable amd64 containerd.io amd64 1.7.24-1 [29.5 MB]
Get:2 http://archive.ubuntu.com/ubuntu focal/universe amd64 pigz amd64 2.4-1 [57.4 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/universe amd64 slirp4netns amd64 0.4.3-1 [74.3 kB]
Get:4 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-buildx-plugin amd64 0.17.1-1~ubuntu.20.04~focal [30.3 MB]
Get:5 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-cli amd64 5:27.3.1-1~ubuntu.20.04~focal [15.0 MB]
Get:6 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce amd64 5:27.3.1-1~ubuntu.20.04~focal [25.6 MB]
Get:7 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-rootless-extras amd64 5:27.3.1-1~ubuntu.20.04~focal [9597 kB]
Get:8 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-compose-plugin amd64 2.29.7-1~ubuntu.20.04~focal [12.6 MB]
Fetched 123 MB in 19s (6527 kB/s)
Selecting previously unselected package pigz.
(Reading database ... 56391 files and directories currently installed.)
Preparing to unpack .../0-pigz_2.4-1_amd64.deb ...
Unpacking pigz (2.4-1) ...
Selecting previously unselected package containerd.io.
Preparing to unpack .../1-containerd.io_1.7.24-1_amd64.deb ...
Unpacking containerd.io (1.7.24-1) ...
Selecting previously unselected package docker-buildx-plugin.
Preparing to unpack .../2-docker-buildx-plugin_0.17.1-1~ubuntu.20.04~focal_amd64.deb ...
Unpacking docker-buildx-plugin (0.17.1-1~ubuntu.20.04~focal) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../3-docker-ce-cli_5%3a27.3.1-1~ubuntu.20.04~focal_amd64.deb ...
Unpacking docker-ce-cli (5:27.3.1-1~ubuntu.20.04~focal) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../4-docker-ce_5%3a27.3.1-1~ubuntu.20.04~focal_amd64.deb ...
Unpacking docker-ce (5:27.3.1-1~ubuntu.20.04~focal) ...
Selecting previously unselected package docker-ce-rootless-extras.
Preparing to unpack .../5-docker-ce-rootless-extras_5%3a27.3.1-1~ubuntu.20.04~focal_amd64.deb ...
Unpacking docker-ce-rootless-extras (5:27.3.1-1~ubuntu.20.04~focal) ...
Selecting previously unselected package docker-compose-plugin.
Preparing to unpack .../6-docker-compose-plugin_2.29.7-1~ubuntu.20.04~focal_amd64.deb ...
Unpacking docker-compose-plugin (2.29.7-1~ubuntu.20.04~focal) ...
Selecting previously unselected package slirp4netns.
Preparing to unpack .../7-slirp4netns_0.4.3-1_amd64.deb ...
Unpacking slirp4netns (0.4.3-1) ...
Setting up slirp4netns (0.4.3-1) ...
Setting up docker-buildx-plugin (0.17.1-1~ubuntu.20.04~focal) ...
Setting up containerd.io (1.7.24-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
Setting up docker-compose-plugin (2.29.7-1~ubuntu.20.04~focal) ...
Setting up docker-ce-cli (5:27.3.1-1~ubuntu.20.04~focal) ...
Setting up pigz (2.4-1) ...
Setting up docker-ce-rootless-extras (5:27.3.1-1~ubuntu.20.04~focal) ...
Setting up docker-ce (5:27.3.1-1~ubuntu.20.04~focal) ...
invoke-rc.d: could not determine current runlevel
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for systemd (245.4-4ubuntu3.24) ...
daniel@MSI:~$ docker --version
Docker version 27.3.1, build ce12230

:::danger
daniel@MSI:~$ sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.
:::

daniel@MSI:~$ sudo systemctl status docker
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
### 問題描述
你的系統顯示運行於 WSL2（Windows Subsystem for Linux 2）環境中。在 WSL2 中，systemd 默認未啟用，這也是為什麼 sudo systemctl 無法正常運行的原因。

在 WSL2 中，必須手動啟動 Docker Daemon，因為它不會像在完整的 Linux 系統中那樣自動運行。

執行以下命令以手動啟動 Docker Daemon：
$ sudo dockerd

每次使用 Docker 時，你需要手動啟動 Docker Daemon (dockerd) 並在另一個終端窗口中執行 Docker 命令。這是因為 WSL 本身並沒有像完整的 Linux 發行版一樣，預設啟動 dockerd 作為系統服務。

after $ sudo dockerd,
$ sudo docker run -it --rm -p 8888:8888 sysprog21/chisel-bootcamp
in another terminal
## Chisel
### Basic types
![image](https://hackmd.io/_uploads/Skyt8S3Q1l.png)
All signed numbers are represented using 2's complement in Chisel.


By default, Chisel will determine the width of your literal to be the minimum necessary. Alternatively, you can provide a width value as a second argument if needed.